Parse XML with PHP - php

This is what my XML looks like
<?xml version="1.0" encoding="UTF-8"?>
<item>
<date>May 2013</date>
<html>http://www.link.to/html.html</html>
</item>
I do this in the PHP and it kind of works
$file = '../modules/xml/nl-archive.xml';
$xml = simplexml_load_file($file);
$string = '';
foreach($xml as $item){
$string .= '<a href="'.$item->html .'" ><li>'.$item->date .'</li></a>';
}
Just trying to get the contents of the date tag and html tag to show up for each of the item tags. Been googling and tinkering, can't get it right. What am I doing wrong?

Try changing your XML to have a root with children like:
<?xml version="1.0" encoding="UTF-8"?>
<items>
<item>
<date>May 2013</date>
<html>http://www.link.to/html.html</html>
</item>
</items>
Php is just fine as you have it.

Related

PHP XML: Getting text of a node and its children

I know that this questions has been asked before, but I cannot make it work. I'm using simplexml and xpath in a PHP file. I need to get text from a node including the text in its child nodes. So, the results should be:
Mr.Smith bought a white convertible car.
Here is the xml:
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="test9.xsl"?>
<items>
<item>
<description>
<name>Mr.Smith bought a <car>white</car> <car>convertible</car> car.</name>
</description>
</item>
</items>
The php that's not working is:
$text = $xml->xpath('//items/item/description/name');
foreach($text as &$value) {
echo $value;
}
Please help!
To get the node value with all its child elements, you can use DOMDocument, with C14n():
<?php
$xml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="test9.xsl"?>
<items>
<item>
<description>
<name>Mr.Smith bought a <car>white</car> <car>convertible</car> car.</name>
</description>
</item>
</items>
XML;
$doc = new DOMDocument;
$doc->loadXML($xml);
$x = new DOMXpath($doc);
$text = $x->query('//items/item/description/name');
echo $text[0]->C14n(); // Mr.Smith bought a white convertible car.
Demo

Replacing innertext of XML node using PHP DOMDocument

I want to replace innertext of a XML node my XML file named test.xml is
<?xml version="1.0" encoding="utf-8"?>
<ads>
<loop>no</loop>
<item>
<description>Description 1</description>
</item>
<item>
<description>Text in item2</description>
</item>
<item>
<description>Let play with this XML</description>
</item>
</ads>
I want to change the value of loop and description tag both,
and it should be saved in test.xml like:
<?xml version="1.0" encoding="utf-8"?>
<ads>
<loop>yes</loop>
<item>
<description>Description Changing Here</description>
</item>
<item>
<description>Changing text in item2</description>
</item>
<item>
<description>We will play later</description>
</item>
</ads>
I tried code in PHP:
<?
$file = "test.xml";
$fp = fopen($file, "rb") or die("cannot open file");
$str = fread($fp, filesize($file));
$dom=new DOMDocument();
$dom->formatOutput = true;
$dom->preserveWhiteSpace = false;
$dom->loadXML($str) or die("Error");
//$dom->load("items.xml");
$root=$dom->documentElement; // This can differ (I am not sure, it can be only documentElement or documentElement->firstChild or only firstChild)
$loop=$root->getElementsByTagName('loop')->item(0);//->textContent;
//echo $loop;
if(trim($loop->textContent)=='no')
{
echo 'ok';
$root->getElementsByTagName('loop')->item(0)->nodeValue ='yes';
}
echo "<xmp>NEW:\n". $dom->saveXML() ."</xmp>";
?>
I tried only for loop tag.I don't know how to replace nodevalue in description tag.
When I run this page it shows output like:
ok
NEW:
<?xml version="1.0" encoding="utf-8"?>
<ads>
<loop>yes</loop>
<item>
<description>Description 1</description>
</item>
<item>
<description>Changing text in item2</description>
</item>
<item>
<description>Let play with this XML</description>
</item>
</ads>
It gives the value yes in browser but don't save it in test.xml any reason?
So, since you have created DOMDocument you can use DOMXpath. Or keep using getElementsByTagName()
You could do this (but only in that context):
$descriptions = $root->getElementsByTagName('description');
foreach($descriptions as $nodeDesciption)
{
$nodeDesciption->nodeValue ='Your custom value';
}

How to add new Child to SimpleXML when one with same name already exists?

I'm trying to add a child to an Simple XML object, but when an element with the same name already exists on that level it doesn't get added.
Here's what I'm trying:
$str = '<?xml version="1.0"?>
<root>
<items>
<item></item>
</items>
</root>';
$xml = new SimpleXMLElement($str);
$xml->addChild('items');
print $xml->asXML();
I get the exact same xml as I started with, when what I really want is a second empty items element. If I use another element name than it does get added.
Use this code for adding a new items node in your example:
$str = '<?xml version="1.0"?>
<root>
<items>
<item></item>
</items>
</root>';
$xml = new SimpleXMLElement($str);
$xml->addChild('items', '');
var_dump($xml->asXML());
Which outputs:
string '<?xml version="1.0"?>
<root>
<items>
<item/>
</items>
<items></items></root>
' (length=109)
You could use simpleloadxml as alternate
$xml = simplexml_load_file("myxml.xml");
$sxe = new SimpleXMLElement($xml->asXML());
$itemsNode = $sxe->items[0];
$itemsNode->addChild("item", $newValue);
$sxe->asXML("myxml.xml");

PHP SimpleXMLElement not parsing <title> node

I have a simple well-formed XML doc that I'm writing to the page using PHP. For some reason the output never includes the title node, and after researching I can't figure this out. If I change the title node to 'heading' or some other name it is included in the output, but when its named 'title', this node is skipped.
Here's the XML doc code...
<?xml version="1.0" encoding="UTF-8"?>
<items>
<product>
<id>cd1</id>
<title>CD One</title>
<description>This is my first CD</description>
<img>/images/sample.jpg</img>
<price>14.99</price>
</product>
</items>
The PHP code looks like this...
<?php
$filename = '../catalog.xml';
$contents = file_get_contents($filename);
echo $contents;
?>
Well, the XML you posted is not valid XML;
The encoding should be in lowercase. Try with this string:
<?xml version="1.0" encoding="utf-8"?>
<items>
<product>
<id>cd1</id>
<title>CD One</title>
<description>This is my first CD</description>
<img>/images/sample.jpg</img>
<price>14.99</price>
</product>
</items>
Validate here: http://validator.w3.org/check

get one value from xml document in php

I have a php variable that contains xml code. I would like to get only one value from that xml and go along.
The xml is:
<?xml version="1.0" encoding="UTF-8" ?> <response> <status>SUCCESS</status> <data><count>1</count> <subscriberlist> <item> <subscriberid>4</subscriberid> <emailaddress>bbbbbb#bbbbbb.bb</emailaddress> <format>h</format> <subscribedate>1314903006</subscribedate> <confirmed>1</confirmed> <unsubscribed>0</unsubscribed> <bounced>0</bounced> <listid>3</listid> </item> </subscriberlist></data></response>
I would like to create the var $subscriberid and get the value (in this case 4)
Can someone explain me?
This doc on php.net will tell you how: http://php.net/manual/en/simplexml.examples-basic.php
For the following xml:
<?xml version="1.0" encoding="UTF-8" ?>
<response>
<status>SUCCESS</status>
<data>
<count>1</count>
<subscriberlist>
<item>
<subscriberid>4</subscriberid>
<emailaddress>bbbbbb#bbbbbb.bb</emailaddress>
<format>h</format>
<subscribedate>1314903006</subscribedate>
<confirmed>1</confirmed>
<unsubscribed>0</unsubscribed>
<bounced>0</bounced>
<listid>3</listid>
</item>
</subscriberlist>
</data>
</response>
If the xml was inside of $xmlstr then to get the subscriberid you would need the following php code:
<?php
$xml = new SimpleXMLElement($xmlstr);
$subscriberid = $xml->data->subscriberlist->item->subscriberid;
?>
You can use simplexml_load_string then parse the data
<?php
$xml = simplexml_load_string('<?xml vers...');
$subscriberid = $xml->data->subscriberlist->item->subscriberid;
?>

Categories