Usuń węzeł elementu XML w PHP

0

Siemka, mam problem z usunięciem elementu w pliku XML, chciałbym usunąć wybrany element "<main>"... 1, 2 lub 3 chodzi o całą zawartość main'a
plik xml:

<main>
<first>1</first>
<second>1</second>
<third>1</third>
<fourth>1</fourth>
<fifth>1</fifth>
</main>
<main>
<first>2</first>
<second>2</second>
<third>2</third>
<fourth>2</fourth>
<fifth>2</fifth>
</main>
<main>
<first>3</first>
<second>3</second>
<third>3</third>
<fourth>3</fourth>
<fifth>3</fifth>
</main>
<main>
<first>4</first>
<second>4</second>
<third>4</third>
<fourth>4</fourth>
<fifth>4</fifth>
</main>
</root>

tutaj mam kod dodawania elementów do pliku xml


if (isset($_POST['insert']))
{
	$xml = new DomDocument("1.0","UTF-8");
	$xml -> load('xml.xml');
	
	$first = $_POST['first'];
	$second = $_POST['second'];
	$third = $_POST['third'];
	$fourth = $_POST['fourth'];
	$fifth = $_POST['fifth'];
	
	$rootTag = $xml ->getElementsByTagName("root")->item(0);
	
	$mainTag = $xml -> createElement("main");
		$firstTag = $xml ->createElement("first", $first);
		$secondTag = $xml ->createElement("second", $second);
		$thirdTag = $xml ->createElement("third", $third);
		$fourthTag = $xml ->createElement("fourth", $fourth);
		$fifthTag = $xml ->createElement("fifth", $fifth);
		
		$mainTag ->appendChild($firstTag);
		$mainTag ->appendChild($secondTag);
		$mainTag ->appendChild($thirdTag);
		$mainTag ->appendChild($fourthTag);
		$mainTag ->appendChild($fifthTag);
		
	$rootTag ->appendChild($mainTag);
	$xml ->save('xml.xml');
}




?>
<html>
<body>

	<form method="POST" action = "insertxml.php">
		Student info <br/>
First: <input type="text" name = "first"><br/>
Second: <input type = "text" name = "second" > <br/>
Third: <input type = "text" name = "third" > <br/>
Fourth: <input type = "text" name = "fourth" > <br/>
Fifth: <input type = "text" name = "fifth" > <br/>
		<input type="submit" name="insert" value="add">
	</form>
		



</body>
</html>

Liczę na rozwiązanie lub wskazówkę.

0

Po wpisaniu w Google nazwy tematu który tutaj podałeś (https://www.google.pl/search?q=Usuń+węzeł+elementu+XML+w+PHP) wyskakuje np. https://itproblemy.pl/questions/44533124/xml-delete-items-in-php
Czytałeś to?

0

Rozwiązanie mojego problemu, może komuś się przyda :)

<?php
    if (isset($_POST['first_tag']))

    {
	     

            $first_tag = $_POST['first_tag'];
            $dom = new DomDocument(); 
            $dom->load("xml.xml"); 
            $xpath = new DomXPath($dom);
            $nodes = $xpath->query('main/first'); 
  
    for ($i = 0; $i < $nodes->length; $i++) 
    {     
        $node = $nodes->item($i);
            if ($node->nodeValue == $first_tag) 
            {
                $node->parentNode->parentNode->removeChild($node->parentNode);
            }         
    }
    echo $dom->saveXML();// show XML on website
    $dom->save('xml.xml'); 
    }
?>

<html>
<head>
</head>
<body>

<br/>
<form action="" method="POST">
	<label>First tag "&lt;first&gt;" :<br/>
		<input type="text" name="first_tag"/>
	</label>
	<button type="submit">DELETE</button>
</form>
</body>
</html>

Pozdrawiam rodziców :*

1 użytkowników online, w tym zalogowanych: 0, gości: 1