XML DOM PHP add a child node to root/links/link - php

I have a little question ... this is my xml:
<?xml version="1.0" encoding="UTF-8"?>
<links>
<link>
<id>432423</id>
<href>http://www.google.ro</href>
</link>
<link>
<id>5432345</id>
<href>http://www.youtube.com</href>
</link>
<link>
<id>5443</id>
<href>http://www.yoursite.com</href>
</link>
</links>
How can i ad another
<link>
<id>5443</id>
<href>http://www.yoursite.com</href>
</link>
??
I managed only to add a record to ROOT/LINKS -> LINK using xpath, and here is the code
<?php
$doc = new DOMDocument();
$doc->load( 'links.xml' );
$links= $doc->getElementsByTagName("links");
$xpath = new DOMXPath($doc);
$hrefs = $xpath->evaluate("/links");
$href = $hrefs->item(0);
$item = $doc->createElement("item");
/*HERE IS THE ISSUE...*/
$link = $doc->createElement("id","298312800");
$href->appendChild($link);
$link = $doc->createElement("link","www.anysite.com");
$href->appendChild($link);
$href->appendChild($item);
print $doc->save('links.xml');
echo "the link has been added!";
?>
Any help would be appreciated :D

$doc = new DOMDocument();
// Setting formatOutput to true will turn on xml formating so it looks nicely
// however if you load an already made xml you need to strip blank nodes if you want this to work
$doc->load('links.xml', LIBXML_NOBLANKS);
$doc->formatOutput = true;
// Get the root element "links"
$root = $doc->documentElement;
// Create new link element
$link = $doc->createElement("link");
// Create and add id to new link element
$id = $doc->createElement("id","298312800");
$link->appendChild($id);
// Create and add href to new link element
$href = $doc->createElement("href","www.anysite.com");
$link->appendChild($href);
// Append new link to root element
$root->appendChild($link);
print $doc->save('links.xml');
echo "the link has been added!";

XPath is used to locate nodes in an XML document, not to manipulate the tree. Try $dom->appendChild($new_link).

Related

XML create node before another PHP

I have a XML file and I'm trying to insert a new node between two others with PHP script.
XML file:
<playlistLog>
<hour>
<track>
<type>take</type>
<url>URL</url>
<title>1473869236.wav</title>
<mix>0</mix>
</track>
(...)
</hour>
</playlistLog>
PHP file:
$xmldoc = new DOMDocument();
$xmldoc->load('../logs/log14-09-2016.xml');
$elem = $xmldoc->getElementsByTagName("track");
$track = $xmldoc->createElement('track');
$type = $xmldoc->createElement('type', 'take');
$track->appendChild($type);
$url = $xmldoc->createElement('url', 'url');
$track->appendChild($url);
$title = $xmldoc->createElement('title', 'title');
$track->appendChild($title);
$mix = $xmldoc->createElement('mix', 'mix');
$track->appendChild($mix);
$xmldoc->documentElement->insertBefore($track,$elem[660]);
$xmldoc->save('../logs/log14-09-2016.xml');
I'm trying to insert the new node before "track" tag number 660 but when I try to open the php file it doesn't work at all.
Can anybody tell me what I am doing wrong?
SOLUTION:
After #ThW response I change a bit what he wrotes and finally the code is doing right:
$document = new DOMDocument();
$document->preserveWhiteSpace = FALSE;
$document->load('../logs/log14-09-2016.xml');
$xpath = new DOMXpath($document);
$previousTrack = $xpath->evaluate('/playlistLog/hour/track')->item(659);
$newTrack = $previousTrack->parentNode->insertBefore($document->createElement('track'),$previousTrack);
$newTrack
->appendChild($document->createElement('type'))
->appendChild($document->createTextNode('take'));
$document->formatOutput = TRUE;
echo $document->save('../logs/log14-09-2016.xml');
$elem[660] is the 661st element node with the tag name track. But its parent node is not the document element. Here is another hour ancestor between. The node you're providing to insertBefore() has a different parent then the node you're adding the new element to.
You can use the $parentNode property to make sure that you append to that node.
Additionally I suggest using Xpath to fetch the track node.
$xml = <<<'XML'
<playlistLog>
<hour>
<track>
<type>take</type>
<url>URL</url>
<title>1473869236.wav</title>
<mix>0</mix>
</track>
</hour>
</playlistLog>
XML;
$document = new DOMDocument();
$document->preserveWhiteSpace = FALSE;
$document->loadXml($xml);
$xpath = new DOMXpath($document);
$previousTrack = $xpath->evaluate('/playlistLog/hour/track[1]')->item(0);
$newTrack = $previousTrack
->parentNode
->insertBefore(
$document->createElement('track'),
$previousTrack
);
$newTrack
->appendChild($document->createElement('type'))
->appendChild($document->createTextNode('take'));
$document->formatOutput = TRUE;
echo $document->saveXml();

how do you add an attibute to root element of XML DOMDocument

i created a XML document using DOMDocument and simpleXML. I need to add an attibute to the root element.
Below is how I created the document and the element. You will note that although the document is initally created by DOMDocument, the child/user nodes are created with simple XML.
$dom = new DOMDocument('1.0','UTF-8');
/*** create the root element ***/
$root = $dom->appendChild($dom->createElement( "Feed" ));
/*** create the simple xml element ***/
$sxe = simplexml_import_dom( $dom );
/*** add a user node ***/
$firstChild = $sxe->addchild("FirstChild");
I tried adding the attibutes to the root like this:
$root = $dom->appendData($dom->createAttribute("extractDate",
"$now"));
but this does not work.
Using DOMDocument, you can set the attribute using the DOMElement::setAttribute method:
$dom = new DOMDocument('1.0','UTF-8');
$root = $dom->createElement("Feed");
$attr = $root->setAttribute("extractDate", "$now"); // <-- here
$dom->appendChild($root);
For SimpleXML, you'll need to use the SimpleXMLElement::addAttribute method:
$sxe = simplexml_import_dom($dom);
$sxe->addAttribute("extractDate", "$now"); // <-- here

Self-closing tags using createElement

I need to add a self-closing tag to XML file with DOM in PHP, but I don't know how, because standardly, this tag looks like this:
<tag></tag>
But it should look like this:
<tag/>
DOM will do that automatically for you
$dom = new DOMDocument;
$dom->appendChild($dom->createElement('foo'));
echo $dom->saveXml();
will give by default
<?xml version="1.0"?>
<foo/>
unless you do
$dom = new DOMDocument;
$dom->appendChild($dom->createElement('foo'));
echo $dom->saveXml($dom, LIBXML_NOEMPTYTAG);
which would then give
<?xml version="1.0" encoding="UTF-8"?>
<foo></foo>
Just pass a node param to DOMDocument::saveXML in order to output only a specific node, without any XML declaration:
$doc = new \DOMDocument('1.0', 'UTF-8');
$doc->preserveWhiteSpace = false;
$doc->formatOutput = false;
$node = $doc->createElement('foo');
// Trimming the default carriage return char from output
echo trim($doc->saveXML($node));
will give
<foo/>
not containing any new line / carriage return ending char.

Need to extract Sub-Tree of XML, before processing with XSLT Processor

I'm processing a XML with XSL with PHP to a HTML output. So far, so good. Here's my code.
<?php
$xslDoc = new DOMDocument();
$xslDoc->load("content.xsl");
$xmlDoc = new DOMDocument();
$xmlDoc->load("content.xml");
// some xpath/dom-query filtering to get
// subtree of loaded xml-file
$proc = new XSLTProcessor();
$proc->importStylesheet($xslDoc);
echo $proc->transformToXML($xmlDoc);
?>
Like you can see, this is a simple XSLT Processor. The output works well. For example I have in the given XML three sibling nodes "node":
<root>
<node>
<subnode>..</subnode>
</node>
<node>
<subnode>..</subnode>
</node>
<node>
<subnode>..</subnode>
</node>
</root>
Now I want just pass the second node through the XML Processor.
How can I do this?
chris
According to http://www.php.net/manual/en/xsltprocessor.transformtodoc.php, the transformToDoc method takes any DOM node as its argument so you could access $xmlDoc->getElementsByTagName('node')->item(1) and pass that to transformToDoc and then use saveXML or saveHTML to get a string of XML or HTML, depending on what kind of result you are looking for.
I solved my task by my own - but thank you for your application.
Here is my code:
$xmlDoc = new DOMDocument();
$xmlDoc->preserveWhiteSpace = false;
$xmlDoc->load("content.xml");
$xpath = new DOMXPath($xmlDoc);
$query = '//ContentData/Content[1]/Headline';
$nodeList = $xpath->query($query);
$newDom = new DOMDocument('1.0','UTF-8');
$root = $newDom->createElement('root');
$root = $newDom->appendChild($root);
foreach ($nodeList as $domElement){
$domNode = $newDom->importNode($domElement, true);
$root->appendChild($domNode);
}
$newDom->saveXML();
$xslDoc = new DOMDocument();
$xslDoc->load("content.xsl");
$proc = new XSLTProcessor();
$proc->importStylesheet($xslDoc);
echo $proc->transformToXML($newDom);

How do I remove a specific node using its attribute value in PHP XML Dom?

My question is best phrase as:
Remove a child with a specific attribute, in SimpleXML for PHP
except I'm not using simpleXML.
I'm new to XML for PHP so I may not be doing the best way
I have a xml created using the $dom->save($xml) for each individual user. (not placing all in one xml due to undisclosed reasons)
It gives me that xml declaration <?xml version="1.0"?> (no idea how to make it to others, but that's not the point, hopefully)
<?xml version="1.0"?>
<details>
<person>name</person>
<data1>some data</data1>
<data2>some data</data2>
<data3>some data</data3>
<category id="0">
<categoryName>Cat 1</categoryName>
<categorydata1>some data</categorydata1>
</category>
<category id="1">
<categoryName>Cat 2</categoryName>
<categorydata1>some data</categorydata1>
<categorydata2>some data</categorydata2>
<categorydata3>some data</categorydata3>
<categorydata4>some data</categorydata4>
</category>
</details>
And I want to remove a category that has a specific attribute named id with the DOM class in php when i run a function activated from using a remove button.
the following is the debug of the function im trying to get to work. Can i know what I'm doing wrong?
function CatRemove($myXML){
$xmlDoc = new DOMDocument();
$xmlDoc->load( $myXML );
$categoryArray = array();
$main = $xmlDoc->getElementsByTagName( "details" )->item(0);
$mainElement = $xmlDoc->getElementsByTagName( "details" );
foreach($mainElement as $details){
$currentCategory = $details->getElementsByTagName( "category" );
foreach($currentCategory as $category){
$categoryID = $category->getAttribute('id');
array_push($categoryArray, $categoryID);
if($categoryID == $_POST['categorytoremoveValue']) {
return $categoryArray;
}
}
}
$xmlDoc->save( $myXML );
}
Well the above prints me an array of [0]->0 all the time when i slot the return outside the if.
is there a better way? I've tried using getElementbyId as well but I've no idea how to work that.
I would prefer not to use an attribute though if that would make things easier.
Ok, let’s try this complete example of use:
function CatRemove($myXML, $id) {
$xmlDoc = new DOMDocument();
$xmlDoc->load($myXML);
$xpath = new DOMXpath($xmlDoc);
$nodeList = $xpath->query('//category[#id="'.(int)$id.'"]');
if ($nodeList->length) {
$node = $nodeList->item(0);
$node->parentNode->removeChild($node);
}
$xmlDoc->save($myXML);
}
// test data
$xml = <<<XML
<?xml version="1.0"?>
<details>
<person>name</person>
<data1>some data</data1>
<data2>some data</data2>
<data3>some data</data3>
<category id="0">
<categoryName>Cat 1</categoryName>
<categorydata1>some data</categorydata1>
</category>
<category id="1">
<categoryName>Cat 2</categoryName>
<categorydata1>some data</categorydata1>
<categorydata2>some data</categorydata2>
<categorydata3>some data</categorydata3>
<categorydata4>some data</categorydata4>
</category>
</details>
XML;
// write test data into file
file_put_contents('untitled.xml', $xml);
// remove category node with the id=1
CatRemove('untitled.xml', 1);
// dump file content
echo '<pre>', htmlspecialchars(file_get_contents('untitled.xml')), '</pre>';
So you want to remove the category node with a specific id?
$node = $xmlDoc->getElementById("12345");
if ($node) {
$node->parentNode->removeChild($node);
}
You could also use XPath to get the node, for example:
$xpath = new DOMXpath($xmlDoc);
$nodeList = $xpath->query('//category[#id="12345"]');
if ($nodeList->length) {
$node = $nodeList->item(0);
$node->parentNode->removeChild($node);
}
I haven’t tested it but it should work.
Can you try with this modified version:
function CatRemove($myXML, $id){
$doc = new DOMDocument();
$doc->loadXML($myXML);
$xpath = new DOMXpath($doc);
$nodeList = $xpath->query("//category[#id='$id']");
foreach ($nodeList as $element) {
$element->parentNode->removeChild($element);
}
echo htmlentities($doc->saveXML());
}
It's working for me. Just adapt it to your needs. It's not intended to use as-is, but just a proof of concept.
You also have to remove the xml declaration from the string.
the above funciton modified to remove an email from a mailing list
function CatRemove($myXML, $id) {
$xmlDoc = new DOMDocument();
$xmlDoc->load($myXML);
$xpath = new DOMXpath($xmlDoc);
$nodeList = $xpath->query('//subscriber[#email="'.$id.'"]');
if ($nodeList->length) {
$node = $nodeList->item(0);
$node->parentNode->removeChild($node);
}
$xmlDoc->save($myXML);
}
$xml = 'list.xml';
$to = $_POST['email'];//user already submitted they email using a form
CatRemove($xml,$to);

Categories