Php Cannot Read Node Value in Xml - php

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>";

Related

How to parse top level XML element php [duplicate]

Using simplexml_load_string() how do I get "ForgotPassword" from the following XML?
<?xml version="1.0" encoding="utf-8"?>
<ForgotPassword>
<version>1.0</version>
<authentication>
<login>username</login>
<apikey>login</apikey>
</authentication>
<parameters>
<emailAddress>joesmith#example.com</emailAddress>
</parameters>
</ForgotPassword>
Are you wanting to get the name of the root node?
$xml = simplexml_load_string($str);
echo $xml->getName();

Display data within XML tags in PHP

The XML that's received is below. How can I get the values ('AI', '3', '20.78'...) and display them in PHP?
The values are always returned in that order but the length can vary.
<?xml version="1.0" encoding="UTF-8"?>
<Data>
<Item Type="AI" Chan="3" Value="20.78" Manual="OFF" Min="0.00" Max="100.00" Units="degC" Name="Workshop Temp" />
</Data>
Any ideas would be much appreciated!
You might find the documentation of SimpleXMLElement::attributes useful,
SimpleXMLElement::attributes — Identifies an element's attributes
Return Values
Returns a SimpleXMLElement object that can be iterated over to loop through the attributes on the tag.
Returns NULL if called on a SimpleXMLElement object that already represents an attribute and not a tag.
here is how you should use it:
$str = <<< XML
<?xml version="1.0" encoding="UTF-8"?>
<Data>
<Item Type="AI" Chan="3" Value="20.78" Manual="OFF" Min="0.00" Max="100.00" Units="degC" Name="Workshop Temp" />
</Data>
XML;
$xml = simplexml_load_string($str);
foreach($xml->Item[0]->attributes() as $key => $att) {
echo $att."\n";
}

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

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