Very simple request (I think) that I have had no luck with.
Here is the contents of the xml file:
<?xml version="1.0" encoding="utf-8"?>
<status USER_ID="xxxxx">OK</status>
Current php:
$xml=simplexml_load_file($file) or die("Error: Cannot create object");
print_r($xml);
Outputs:
SimpleXMLElement Object ( [#attributes] => Array ( [USER_ID] => xxxxx ) [0] => OK )
And now I'm stuck
How can I get the value of USER_ID and that the status was "OK" into my php script.
Thanks.
Try this one below
echo "Display the user id: " . $xml['USER_ID'];
echo "Display the status: " . $xml[0];
Hope this will help you.
If you don't like SimpleXml (like me), you can also use the XMLReader Class like:
$XMLReader = new XMLReader;
$XMLReader->XML(file_get_contents($file)); //you can use $XMLReader->open('file://'.$file); too
//move to first node
$XMLReader->read();
//get an attribute
echo "USER_ID:".$XMLReader->getAttribute('USER_ID')."\n";
//get the contents of the tag as a string
echo "Status:" .$XMLReader->readString();
Output:
USER_ID:xxxxx
Status:OK
Sandbox
Related
Im trying to get the catId value. But i can see only the category value.
My xml file looks below:
<sample>
<Item ItemNumber="00000088" FormattedItemNumber="00000-088">
<CompatibleModels />
<Category CatId="160" > test 123 </Category>
<Images />
<Documents />
<RequiredItems />
</Item>
</sample>
$xml = simplexml_load_file("test.xml");
print_r($xml);
[sample] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[ItemNumber] => 00000088
[FormattedItemNumber] => 00000-088
)
[Category] => Bags/Luggage 123
)
how can get the CatId value? Why the cateId value is missing?
You can do it by many ways. Let's try-
foreach ($xml as $items) {
echo $items->Category['CatId'];
}
WORKING DEMO: https://3v4l.org/Onqe2
print_r doesn't really work with SimpleXML objects. But from the sample data you have provided you can simply access the CatId attribute using
echo $xml->Item->Category['CatId'];
You can loop and get it by using following snippet, please refer inline documentation for explanation
$xml1 = simplexml_load_file("test.xml") or die("Error: Cannot create object");
foreach ($xml1->children() as $items1) { // children mean item
echo ($items1->category['catid']); // for category tag get catid attribute
}
I have a XML file named conf.xml and I'm trying to display the contents of this XML file via a simple php script (placed in the same directory) as follows:
conf.xml
<?xml version="1.0" encoding="UTF-8"?>
<registration_info>
<organization name="Home" />
</registration_info>
PHP script:
$data=simplexml_load_file("conf.xml");
$node=$data->registration_info;
$subnode=$node->organization;
echo (string) $subnode['name']; // Displays null string
I feel that there's nothing wrong with the code but the output is unexpected as the anticipated output was "Home". Can anyone please help me solve this problem and explain me the solution?
Thanks in advance.
The existing answer given here is correct, but the explanations are rather confused. SimpleXML is not hiding the root node of your XML, it's just that the object you have already is that node.
Each SimpleXMLElement object represents a particular node in the XML document's tree. There is no separate object in SimpleXML representing "the whole document", so when you run simplexml_load_file, the object returned is the SimpleXMLElement for the root node.
$root_node = simplexml_load_file("conf.xml");
echo $root_node->getName(); // registration_info
$child_node = $root_node->organization;
// Short for $root_node->organization[0];
// meaning "get the first child with name 'organization'
echo $child_node->getName(); // organization
Try this hope this will help you out. You have to just remove $node=$data->registration_info;
Try this snippet here
<?php
ini_set('display_errors', 1);
$data=simplexml_load_file("conf.xml");
$subnode=$data->organization;
echo (string) $subnode['name'];
Output: print_r($data)
SimpleXMLElement Object
(
[organization] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => Home
)
)
)
How do I get the entire result from XML using simplexml_load_string?
My XML looks like this
<xml>
<code>
<codedesc>Code Name</codedesc>
100
</code>
<code>
<codedesc>Code Name 2</codedesc>
200
</code>
</xml>
When I load it with PHP using this code
$XML = simplexml_load_string($data);
print_r($XML);
I only get codedesc and not the code number as you can see here
https://eval.in/51562
You need to wrap your values in XML so that they can be accessed.
<xml>
<code>
<codedesc>Code Name</codedesc>
<value>100</value>
</code>
<code>
<codedesc>Code Name 2</codedesc>
<value>200</value>
</code>
</xml>
You can then access them like so:
$xml = simplexml_load_string($data);
echo $xml->code[0]->value; //100
echo $xml->code[1]->value; //200
echo $xml->code[1]->codedesc; //Code Name 2
If you can't change your xml, the following should work:
echo $xml->code[0]; //100
echo $xml->code[1]; //200
I'm not sure what you're trying to get SimpleXML correctly parses your XML string, and returns a class with an array called 'code', that has 2 objects called codedesc:
SimpleXMLElement Object
(
[code] => Array
(
[0] => SimpleXMLElement Object
(
[codedesc] => Code Name
)
[1] => SimpleXMLElement Object
(
[codedesc] => Code Name 2
)
)
)
If you're trying to get to the values (100, 200) - try putting them in XML elements.
If you're trying to print the entire XML, try print_r($XML->asXML()) (see this)
I am importing a XML file that has an amount field <amount>$10.00</amount> but when it is read in using code I got from your other posts, the value is returned as .00.
Using:
$xml = simplexml_load_file("testInput.xml");
print_r($xml);
Result:
[amount] => .00
I can't find anywhere why this is failing... Unless it has to do with the $ or period in the value field but I can't find anything about reserved characters.
I tried to duplicate your results and couldn't...
I created a file called xml_test.php:
<?php
$xml = simplexml_load_file('test_input.xml');
print_r($xml);
?>
Then built the XML (test_input.xml):
<?xml version="1.0" encoding="UTF-8"?>
<tests>
<test>
<amount>$10.00</amount>
</test>
</tests>
And this was my result in the browser:
SimpleXMLElement Object ( [test] => SimpleXMLElement Object ( [amount] => $10.00 ) )
Is there anything else going on or am I missing something? Maybe you can paste in your XML...
I have an xml file
<?xml version="1.0" encoding="utf-8"?>
<xml>
<events date="01-10-2009" color="0x99CC00" selected="true">
<event>
<title>You can use HTML and CSS</title>
<description><![CDATA[This is the description ]]></description>
</event>
</events>
</xml>
I used xpath and and xquery for parsing the xml.
$xml_str = file_get_contents('xmlfile');
$xml = simplexml_load_string($xml_str);
if(!empty($xml))
{
$nodes = $xml->xpath('//xml/events');
}
i am getting the title properly, but iam not getting description.How i can get data inside
the cdata
SimpleXML has a bit of a problem with CDATA, so use:
$xml = simplexml_load_file('xmlfile', 'SimpleXMLElement', LIBXML_NOCDATA);
if(!empty($xml))
{
$nodes = $xml->xpath('//xml/events');
}
print_r( $nodes );
This will give you:
Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[date] => 01-10-2009
[color] => 0x99CC00
[selected] => true
)
[event] => SimpleXMLElement Object
(
[title] => You can use HTML and CSS
[description] => This is the description
)
)
)
You are probably being misled into thinking that the CDATA is missing by using print_r or one of the other "normal" PHP debugging functions. These cannot see the full content of a SimpleXML object, as it is not a "real" PHP object.
If you run echo $nodes[0]->Description, you'll find your CDATA comes out fine. What's happening is that PHP knows that echo expects a string, so asks SimpleXML for one; SimpleXML responds with all the string content, including CDATA.
To get at the full string content reliably, simply tell PHP that what you want is a string using the (string) cast operator, e.g. $description = (string)$nodes[0]->Description.
To debug SimpleXML objects and not be fooled by quirks like this, use a dedicated debugging function such as one of these: https://github.com/IMSoP/simplexml_debug
This could also be another viable option, which would remove that code and make life a little easier.
$xml = str_replace("<![CDATA[", "", $xml);
$xml = str_replace("]]>", "", $xml);