get one value from xml document in php - 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;
?>

Related

Php Cannot Read Node Value in Xml

I cannot able to read node.I want to read the USER_ID node value.When i am trying to read with simplexml_load_string.
I am unable to read the node value.
This is my xml code :
<?xml version="1.0" encoding="utf-16" standalone="yes"?>
<RESULT>
<SUCCESS>1</SUCCESS>
<ERRMESSAGE />
<REMARKS />
<DATA>
<data>
<USERS>
<U>
<USER_ID>1</USER_ID>
</U>
</data>
</DATA>
</RESULT>
Your xml file is not valid. Change encoding of xml document to utf-8.
Then just access like normal object:
$xmlfile = file_get_contents('test.xml');
$array = simplexml_load_string($xmlfile);
echo "<pre>";
print_r($arr->DATA->data->USERS->U->USER_ID);
echo "</pre>";

SimpleXMLElement creating two XML Tags

I'm building xml with the SimpleXMLElement Object from PHP.
While doing so I encountered the following problem, which i can't solve:
I'm generating the root xml element like this:
$xml = new SimpleXMLElement("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
But i get a 2 XML headers when i do echo $xml->asXML(); like this:
<?xml version="1.0"?>
<xml version="1.0" encoding="UTF-8"></xml>
Which is obvioulsy wrong. But how can i fix this so i only get the
<xml version="1.0" encoding="UTF-8">
part?
You must also supply the surrounding tag.
For example:
$xml = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8" ?> <BASETAG />');

Parse XML with 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.

php : xml node content edit and return xml

Here is my XML<response> <statusCode>200</statusCode> <statusText>OK</statusText> <data> <getAssetResponse> <assetId>89898</assetId> <content> some text with HTML content </content> </getAssetResponse> </data></response>
In my php, I need to replace content node substr (HTML with xhtml) and return the XML with same structure.
<?php $file = file_get_contents("filx.xml"); $doc = DOMDocument::loadXML($file); $data = $dom->getElementsByTagName("data"); foreach($data as $node){echo "hello";}
my simple start isn't working...What do i need to do to get the node content?
If you only need to replace the content of the content-node, it is maybe easier to use SimpleXML, like this:
<?
$xml_object = simplexml_load_file("test.xml");
$xml_object->data->getAssetResponse->content = "Test 123";
print $xml_object->asXML();
?>
Result:
<?xml version="1.0"?>
<response>
<statusCode>200</statusCode>
<statusText>OK</statusText>
<data>
<getAssetResponse>
<assetId>89898</assetId>
<content>Test 123</content>
</getAssetResponse>
</data>
</response>

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

Categories