Need to copy specific XML node from one doc to another - php

I have two XML files (that have a number of common nodes) that look a bit like these:
DESTINATION FILE: ('destination.xml')
<items>
<item>
<title>Item A</title>
<description>This is the description for Item A</description>
<id>1001</id>
</item>
<item>
<title>Item B</title>
<description>This is the description for Item B</description>
<id>1002</id>
</item>
<item>
<title>Item D</title>
<description>This is the description for Item D</description>
<id>1004</id>
</item>
and
SOURCE FILE: ('source.xml')
<items>
<item>
<title>Item A</title>
<description>This is the description for Item A</description>
<id>1001</id>
</item>
<item>
<title>Item C</title>
<description>This is the description for Item C</description>
<id>1003</id>
</item>
<item>
<title>Item B</title>
<description>This is the description for Item B</description>
<id>1002</id>
</item>
I need to grab the node from SOURCE with the 'id' matching '1003' (in this example) and import it into the DESTINATION. I'm looking for insight in using importNode (or a simpleXML option) and also the xpath in only getting only the node I need.

Just do that and it should work :
<?php
header('Content-type: application/xml'); //Just to test in the browser directly and have a good format
$docSource = new DOMDocument();
$docSource->loadXML(file_get_contents('source.xml'));
$docDest = new DOMDocument();
$docDest->loadXML(file_get_contents('destination.xml'));
$xpath = new DOMXPath($docSource);
$result = $xpath->query('//item[id=1003]')->item(0); //Get directly the node you want
$result = $docDest->importNode($result, true); //Copy the node to the other document
$items = $docDest->getElementsByTagName('items')->item(0);
$items->appendChild($result); //Add the copied node to the destination document
echo $docDest->saveXML();

To get the correct node, I think your XPath should look like this:
$xpath->query('/items/item/id[.="1003"]/..')
To import it to the other document, you'll need to create the document and call importNode with the second parameter set to true:
$newDom = new DOMDocument;
$newDom->load('destination.xml');
$newNode = $newDom->importNode($el, true);
$newDom->firstChild->appendChild($newNode);
file_put_contents('destination.xml', $newDom->saveXML());

Related

PHP - Replace child node in XML

I want to replace a child node name in the XML through PHP and also want to append the text in the child node as well. Can you please help me to replace the child node name and append text in the same node in PHP?
PHP Code:
$doc = new DOMDocument;
$doc->load('abc.xml');
$thedocument = $doc->documentElement;
$xpath = new DOMXPath($doc);
// Need a code to replace the name of child node "<id>" to "<link>" and append text "abc.com/" in the node <id>
$doc->formatOutput = true;
$result1 = $doc->saveXML();
$doc->save('abc.xml');
XML (Current):
<rss>
<data>
<item>
<id>1122</id>
<title>Test 123</title>
</item>
</data>
</rss>
XML (Needed):
<rss>
<data>
<item>
<link>abc.com/?qs=1122</link>
<title>Test 123</title>
</item>
</data>
</rss>

Merge two variable SimpleXML Objects in PHP

I've been trying to merge two XML files I use to build my menubar in my web application for hours, but I can't get it to work.
I have my main XML file which looks like this:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<root>
<version>1.0.0</version>
<menu>
<Category1>
<item>
<id>Cake</id>
<nr>1</nr>
<hint>I like these</hint>
<userlevel>5</userlevel>
</item>
<item>
<id>Cake 2</id>
<nr>2</nr>
<hint>I like these too, but only for me</hint>
<userlevel>10</userlevel>
</item>
<Category1>
<Category2WithApples>
<item>
<id>Apple Cake</id>
<nr>1</nr>
<hint>Sweet</hint>
<userlevel>5</userlevel>
</item>
<item>
<id>Rainbow Cake</id>
<nr>2</nr>
<hint>Mine!!</hint>
<userlevel>10</userlevel>
</item>
<Category2WithApples>
</menu>
</root>
Now, I want each user to be able to load in his custom XML which is in the same folder as the main.xml which looks like this:
<CategoryMyOwn>
<item>
<id>Item in my Category</id>
<nr>0</nr>
<hint>Some text</hint>
<userlevel>0</userlevel>
</item>
</CategoryMyOwn>
<Category1>
<item>
<id>Item in existing category</id>
<nr>0</nr>
<hint>Some text</hint>
<userlevel>0</userlevel>
</item>
</Category1>
I've tried solutions from
http://php.net/manual/de/ref.simplexml.php
php recursion, function doesn't return any value
http://durgagupta.com.np/php-how-to-merge-two-simplexml-objects/
but they all do not work at all for me or just append the second file to the end of my main.xml. So, my question is, how do I properly merge the user.xml into my main.xml so it looks like this:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<root>
<version>1.0.0</version>
<menu>
<Category1>
<item>
<id>Cake</id>
<nr>1</nr>
<hint>I like these</hint>
<userlevel>5</userlevel>
</item>
<item>
<id>Cake 2</id>
<nr>2</nr>
<hint>I like these too, but only for me</hint>
<userlevel>10</userlevel>
</item>
<item>
<id>Item in existing category</id>
<nr>0</nr>
<hint>Some text</hint>
<userlevel>0</userlevel>
</item>
<Category1>
<Category2WithApples>
<item>
<id>Apple Cake</id>
<nr>1</nr>
<hint>Sweet</hint>
<userlevel>5</userlevel>
</item>
<item>
<id>Rainbow Cake</id>
<nr>2</nr>
<hint>Mine!!</hint>
<userlevel>10</userlevel>
</item>
<Category2WithApples>
<CategoryMyOwn>
<item>
<id>Item in my Category</id>
<nr>0</nr>
<hint>Some text</hint>
<userlevel>0</userlevel>
</item>
</CategoryMyOwn>
</menu>
</root>
Your second XML is not a document, XML documents need to have a document element node. In other words here at the top level only a single element node is allowed. All other element nodes have to be descendants of that node.
You can treat this as an XML fragment however. A fragment is the inner XML of an element node.
In both cases it easier to use DOM for that.
Append a fragment to a parent element node
Let's keep it simple for the first step and append the fragment to the menu node.
$document = new DOMDocument();
$document->loadXml($targetXml);
$xpath = new DOMXpath($document);
$fragment = $document->createDocumentFragment();
$fragment->appendXml($fragmentXml);
foreach ($xpath->evaluate('/root/menu[1]') as $menu) {
$menu->appendChild($fragment);
}
echo $document->saveXml();
The Xpath expression can /root/menu[1] selects the first menu element node inside the root. This can be only one node or none.
A document fragment in DOM is a node object and can be appended like any other node (element, text, ...).
Merging nodes
Merging the category nodes is a little more difficult. But Xpath will help.
$document = new DOMDocument();
$document->loadXml($targetXml);
$xpath = new DOMXpath($document);
$fragment = $document->createDocumentFragment();
$fragment->appendXml($fragmentXml);
$menu = $xpath->evaluate("/root/menu[1]")->item(0);
foreach ($xpath->evaluate('*', $fragment) as $category) {
$targets = $xpath->evaluate("{$category->nodeName}[1]", $menu);
if ($targets->length > 0) {
$targetCategory = $targets->item(0);
foreach ($category->childNodes as $item) {
$targetCategory->appendChild($item);
}
} else {
$menu->appendChild($category);
}
}
echo $document->saveXml();
Fetching the menu node
$menu = $xpath->evaluate("/root/menu[1]")->item(0);
This is about the same like in the first simple example. It fetch the menu nodes in root and returns the first found node. You should check if the list contained a node. But for this example just take it for guaranteed.
Iterating the fragment
foreach ($xpath->evaluate('*', $fragment) as $category) {
...
}
* is a simple Xpath expression that returns any element child node. The fragment can contain other nodes (whitespace, text, comment, ...). The second argument for DOMXpath::evaluate() is the context for the Xpath expression.
Fetching the target category
Next you need to fetch the category node with the same name from the target document. This will return a list with one node or an empty list.
$targets = $xpath->evaluate("{$category->nodeName}[1]", $menu);
if ($targets->length > 0) {
...
} else {
...
}
Append to the found target category
If the category exists append all child nodes from the category in the fragment to the target.
$targetCategory = $targets->item(0);
foreach ($category->childNodes as $item) {
$targetCategory->appendChild($item);
}
Append a category
$menu->appendChild($category);
If the category doesn't exists, just append it to the menu.

PHP Copy xml nodes from one doc to another

First of all, I need to Find a parent node by a specific child node value in an xml doc; Then copy some specific children nodes from the parent node to another xml doc.
For instance:
DESTINATION FILE: ('destination.xml')
<item>
<offerStartDate>2012-15-02</offerStartDate>
<offerEndDate>2012-19-02</offerEndDate>
<title>Item Title</title>
<rrp>14.99</rrp>
<offerPrice>9.99</offerPrice>
</item>
and
SOURCE FILE: ('source.xml')
<items>
<item>
<title>Item A</title>
<description>This is the description for Item A</description>
<id>1003</id>
<price>
<rrp>10.00</rrp>
<offerPrice>4.99</offerPrice>
</price>
<offer>
<deal>
<isLive>0</isLive>
</deal>
</offer>
</item>
<item>
<title>Item B</title>
<description>This is the description for Item B</description>
<id>1003</id>
<price>
<rrp>14.99</rrp>
<offerPrice>9.99</offerPrice>
</price>
<offer>
<deal>
<isLive>1</isLive>
</deal>
</offer>
</item>
<item>
<title>Item C</title>
<description>This is the description for Item C</description>
<id>1003</id>
<price>
<rrp>9.99</rrp>
<offerPrice>5.99</offerPrice>
</price>
<offer>
<deal>
<isLive>0</isLive>
</deal>
</offer>
</item>
I want to find the parent node <item> that has it's child node <isLive> value set to "1". Then copy other children nodes of the parent node to the destination xml.
e.g. If parent node <item> has its child node <isLive> set to 1. Copy <title>, <rrp> and <offerPrice> nodes and their values and add them to the destination file as children nodes as shown above.
Pardon my technical lingo if I have not used them correctly.
Many thanks for the help guys!
With SimpleXml (demo):
$dItems = simplexml_load_file('destination.xml');
$sItems = simplexml_load_file('source.xml');
foreach ($sItems->xpath('/items/item[offer/deal/isLive=1]') as $item) {
$newItem = $dItems->addChild('item');
$newItem->addChild('title', $item->title);
$newItem->addChild('rrp', $item->price->rrp);
$newItem->addChild('offerprice', $item->price->offerPrice);
}
echo $dItems->saveXML();
With DOM (demo):
$destination = new DOMDocument;
$destination->preserveWhiteSpace = false;
$destination->load('destination.xml');
$source = new DOMDocument;
$source->load('source.xml');
$xp = new DOMXPath($source);
foreach ($xp->query('/items/item[offer/deal/isLive=1]') as $item)
{
$newItem = $destination->documentElement->appendChild(
$destination->createElement('item')
);
foreach (array('title', 'rrp', 'offerPrice') as $elementName) {
$newItem->appendChild(
$destination->importNode(
$item->getElementsByTagName($elementName)->item(0),
true
)
);
}
}
$destination->formatOutput = true;
echo $destination->saveXml();

SimpleXML adding child with children and attributes

I have some XML to which I need to add a child.
Using SimpleXML, I'm not having any issue adding a simple node.
The beginning XML looks a bit like this:
<root>
<item>
<title>This is the title</title>
<sort>2</sort>
</item>
<item>
<title>This is another title</title>
<sort>3</sort>
</item>
</root>
I need to add a node that looks like this:
<label id=1>
<title type=normal>This is a label</title>
<sort>1</sort>
</label>
The result would be:
<root>
<item>
<title>This is the title</title>
<sort>2</sort>
</item>
<item>
<title>This is another title</title>
<sort>3</sort>
</item>
<label id=1>
<title type=normal>This is a label</title>
<sort>1</sort>
</label>
</root>
I'm able to add a simple child using:
$xml->root->addChild('label', 'This is a label');
I am having trouble getting the attributes and children added to this newly added node though.
I am not worried about appending versus prepending as the sorting happens in XSLT.
addChild returns the added child, so you just have to do :
$label = $xml->root->addChild('label');
$label->addAttribute('id', 1);
$title = $label->addChild('title', 'This is a label');
$title->addAttribute('type', 'normal');
$label->addChild('sort', 1);
$xml->root->addChild('label', 'This is a label');
This opereration returns a reference to the child that was just added. So you could do this:
$child = $xml->root->addChild('label', 'This is a label');
From this, you can not add your additional children and attributes to that child.
$child->addAttributes('id', '1');
Since it returns a reference, that node and attributes that were just added are part of the $xml object as well.

Accessing a single XML DOM Document node

I am completely new to DOM Documents, basically what I am trying to do, is to load a RSS feed in and select only one node, and then save it to a XML file.
Here is the XML I am loading from a web feed:
<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
<channel>
<title>Markets</title>
<description/>
<link>http://www.website.com</link>
<language>en-us</language>
<copyright>XML Output Copyright</copyright>
<ttl>15</ttl>
<pubDate>Tue, 16 Nov 2010 09:38:00 +0000</pubDate>
<webMaster>admin#website.com</webMaster>
<image>
<title>title</title>
<url>http://www.website.com/images/xmllogo.gif</url>
<link>http://www.website.com</link>
<width>144</width>
<height>16</height>
</image>
<item>
<title>title</title>
<description>the description goes here
</description>
<enclosure url="http://www.website.com/images/image.png" type="image/png"/>
</item>
</channel>
</rss>
Here is my lame attempt at getting the <description> node and saving it to feed.xml:
<?php
$feed = new DOMDocument();
$feed->load('http://www.website.com/directory/directory/cz.c');
$nodeValue = $feed->getElementsByTagName('description')->item(0)->nodeValue;
$feed->save("feed.xml");
?>
So basically I need to get the description tag, and save it as a XML file.
Any help would be appreciated, thanx in advance!
Almost correct. To get the "outerXml" of a node, you can pass the node to saveXml()
$feed = new DOMDocument();
$feed->load('http://www.website.com/directory/directory/cz.c');
$xml = $feed->saveXml($feed->getElementsByTagName('description')->item(0));
file_put_contents("feed.xml", $xml);
Saving with file_put_contents will not include an XML prolog. Note that in your example, the first description element is empty, so the file will contain <description/>.
If you want to extract the node as standalone XML Document, you have to instantiate a new DOMDocument and import the DOMNode and then use save().
$dom = new DOMDocument($feed->xmlVersion, $feed->xmlEncoding);
$dom->appendChild(
$dom->importNode(
$feed->getElementsByTagName('description')->item(0),
TRUE
)
);
echo $dom->save('new.xml');
$feed = simplexml_load_file('feed.xml');
$descr=$feed->channel->description;
Try this

Categories