editing xml data in php - php

I want this code to replace in the "platform" tag with something like "newData"
$xml = new DOMDocument();
$xml->load('myhappyfarmsave.xml');
$xpath = new DOMXPath($xml);
$elements = $xpath->query('/myhappyfarmsave/platform');
$domElement = $elements->item(0);
var_dump($domElement->textContent);
$domElement->textContent = "newData";
$xml->save('myhappyfarmsavenew.xml');
I don't know what's wrong with my code. thanks for your help
<?xml version="1.0" encoding="UTF-8"?>
<myhappyfarmsave version="3">
<platform>Bazaar</platform>
<timestamp>1356876534</timestamp>
<lastlogtime>1356823542</lastlogtime>
</myhappyfarmsave>

Well, $node->textContent is a readonly value, so you cannot modify it.
Try playing with $node->nodeValue instead:
$domElement = $elements->item(0);
$domElement->nodeValue = "newData";
$xml->save('myhappyfarmsavenew.xml');

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();

Fetch value from XML Object?

I need to fetch the value of "joinmeetingurl" element from the xml. I tried in following way. But it returns nothing. Please help me to fetch the value.
<?php
$xml = '<serv:message xmlns:serv="http://www.webex.com/schemas/2002/06/service"
xmlns:com="http://www.webex.com/schemas/2002/06/common"
xmlns:meet="http://www.webex.com/schemas/2002/06/service/meeting"
xmlns:att="http://www.webex.com/schemas/2002/06/service/attendee">
<serv:header>
<serv:response>
<serv:result>SUCCESS</serv:result>
<serv:gsbstatus>PRIMARY</serv:gsbstatus>
</serv:response>
</serv:header>
<serv:body>
<serv:bodycontent xsi:type="meet:getjoinurlMeetingResponse"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<meet:joinmeetingurl>meetingURL</meet:joinmeetingurl>
</serv:bodycontent>
</serv:body>
</serv:message>';
$xml = simplexml_load_string($xml);
$items = $xml->registerXPathNamespace('meet','http://www.webex.com/schemas/2002/06/service/meeting');
$resp = $xml->xpath('//meet:joinmeetingurl');
?>
Im getting empty value for $resp always.
Your XPath should've worked, and you can cast the element to string to get the value, for example :
$xml = <<<XML
<serv:message xmlns:serv="http://www.webex.com/schemas/2002/06/service"
xmlns:com="http://www.webex.com/schemas/2002/06/common"
xmlns:meet="http://www.webex.com/schemas/2002/06/service/meeting"
xmlns:att="http://www.webex.com/schemas/2002/06/service/attendee">
<serv:header>
<serv:response>
<serv:result>SUCCESS</serv:result>
<serv:gsbstatus>PRIMARY</serv:gsbstatus>
</serv:response>
</serv:header>
<serv:body>
<serv:bodycontent xsi:type="meet:getjoinurlMeetingResponse"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<meet:joinmeetingurl>meetingURL</meet:joinmeetingurl>
</serv:bodycontent>
</serv:body>
</serv:message>
XML;
$xml = simplexml_load_string($xml);
$xml->registerXPathNamespace('meet','http://www.webex.com/schemas/2002/06/service/meeting');
$resp = $xml->xpath('//meet:joinmeetingurl');
echo (string)$resp[0];
eval.in demo
output :
meetingURL
Can offer no guidance with simplexml and associated functions but it seems quite simple with standard DOMDocument and DOMXPath
$dom=new DOMDocument;
$dom->loadXML( $xml );
$xpath=new DOMXPath( $dom );
$col=$xpath->query('//meet:joinmeetingurl');
foreach( $col as $node )echo $node->nodeValue;
$dom=null;

PHP - Importing XML Node

I'm trying to import one node from one document to another. This throws an 'Wrong Document Error' error:
$xml = <<<XML
<root>
<child1>foo </child1>
<child2> bar</child2>
</root>
XML;
$doc = new DOMDocument;
$new = new DOMDocument;
$doc->loadXML($xml);
$xp = new DOMXPath($doc);
foreach($xp->query('/root/node()') as $element)
{
$new->appendChild($doc->importNode($element, true));
}
echo $new->saveXML();
You want to use $new->importNode... rather than $doc->importNode... as $new is the document you're trying to add the node to.

creating xml file

I am creating xml file as below, can anybody help how to get that using php?
xml file:
<products>
<product id="123" />
</products>
Php file:
i am using following code but not getting result as above
$xml = new DomDocument('1.0', 'utf-8');
$xml->formatOutput = true;
$products= $xml->createElement('products');
$product = $xml->createElement('product');
$id = $xml->createElement('id');
$xml->appendChild($products);
$products->appendChild($product);
$product->appendChild($id);
$id->nodeValue = 123;
$xml->save("data.xml");
retrieving xml data:
$xml = new DomDocument();
$xmlFile = "data.xml";
$xml= DOMDocument::load($xmlFile);
$product = $xml->getElementsByTagName("product");
foreach($product as $node)
{
$address = $node->getElementsByAttributeName("address");
$address = $address->item(0)->nodeValue;
echo"$address";
}
You are not getting the results you want because you're adding id as a tag rather than an attribute. I just refactored your code a little and made id an attribute. I tested the code below and it produces your desired result.
<?php
$xml = new DomDocument('1.0', 'utf-8');
$xml->formatOutput = true;
$products= $xml->createElement('products');
$product = $xml->createElement('product');
$xml->appendChild($products);
$products->appendChild($product);
$product->appendChild(new DomAttr('id', '123'));
$xml->save("data.xml");
?>
If id is to be an attribute, then you need to use the DOMElement::setAttribute() method. I think this will work - according to the documentation, if an attribute doesn't already exist it will be created.
$product->setAttribute("id", "123");
Then loas the $product->appendChild( $id );

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