Getting exact data from xml to array [PHP] - php

i m trying to get some data from the xml out of prestashop api
To be exact, i want to create an array which will have as key the id (ex. 2) and value the name->language.
I am using simplexml_load_string to convert the xml to arrays
<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<category>
<id>
<![CDATA[2]]>
</id>
...
<name>
<language id="1" xlink:href="https://mosty.com/api/languages/1">
<![CDATA[Home]]>
</language>
</name>
<link_rewrite>
<language id="1" xlink:href="https://mosty.com/api/languages/1">
<![CDATA[home]]>
</language>
</link_rewrite>
...
</category>
</prestashop>

Please have a look at the code below. I suppose you have multiple <category> elements:
Code
$xml = '<?xml version="1.0" encoding="UTF-8"?>
<prestashop xmlns:xlink="http://www.w3.org/1999/xlink">
<category>
<id>
<![CDATA[2]]>
</id>
<name>
<language id="1" xlink:href="https://mosty.com/api/languages/1">
<![CDATA[Home]]>
</language>
</name>
<link_rewrite>
<language id="1" xlink:href="https://mosty.com/api/languages/1">
<![CDATA[home]]>
</language>
</link_rewrite>
</category>
</prestashop>';
$obj = simplexml_load_string($xml);
$result = [];
foreach($obj->category as $item) {
$result[trim($item->id)] = trim($item->name->language);
}
var_dump($result);
Output
array(1) { [2]=> string(4) "Home" }

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

PHP String to Valid XML file

I have a problem converting my string which has a structure of XML into a proper XML file.
My String looks like:
<product>
<ID>12345</ID>
<NAME></NAME>
</product>
<product>
<ID>123</ID>
<NAME></NAME>
</product>
And so on. The problem is that I get empty result if I use DOM.
$dom = new DomDocument('1.0', 'UTF-8');
$dom->loadXML($products);
$xml = $dom->saveXML($dom);
Output is:
string(39) "<?xml version="1.0" encoding="UTF-8"?>
"
How can I make this work? Or I just can add the html and root tags to this string and just parse it to the file?
Your XML is not properly formatted. XML requires a root element.
If you change your XML to something like this:
<products>
<product>
<ID>12345</ID>
<NAME></NAME>
</product>
<product>
<ID>123</ID>
<NAME></NAME>
</product>
</products>
It should work as expected.
Adjusted code:
<?php
$dom = new DomDocument('1.0', 'UTF-8');
$dom->loadXML('<products>
<product>
<ID>12345</ID>
<NAME></NAME>
</product>
<product>
<ID>123</ID>
<NAME></NAME>
</product>
</products>');
echo $dom->saveXML();
Outputs:
<?xml version="1.0"?>
<products>
<product>
<ID>12345</ID>
<NAME/>
</product>
<product>
<ID>123</ID>
<NAME/>
</product>
</products>

How to parse XML-file to array and retrieve all children of an child with specific attribute in PHP?

my XML-file is as follows: agents.xml
<?xml version="1.0" encoding="UTF-8"?>
<agents>
<agent id="1">
<aname>pi1</aname>
<alive>0</alive>
<scenarios>1,2,3</scenarios>
</agent>
<agent id="2">
<aname>pi2</aname>
<alive>1</alive>
<scenarios>4,5,6</scenarios>
</agent>
</agents>
I want to retrieve all child elements of an agent, selected by attribute value "id". I tried the following, passing a the variable "id" to the script:
$agents_xml = simplexml_load_file("/<path_to>/agents.xml");
$json = json_encode($agents_xml);
$array = json_decode($json,TRUE);
//decrement id for correct index
$id=$id-1;
//I want to return in JSON format
$testarr=json_encode($array['agent'][$id]);
echo $testarr;
When "id" has value 1, i got this:
{"#attributes":{"id":"1"},"aname":"pi1","alive":"0","scenarios":"1,2,3"}
But i know, if the XML is disordered like:
<?xml version="1.0" encoding="UTF-8"?>
<agents>
<agent id="2">
<aname>pi2</aname>
<alive>1</alive>
<scenarios>4,5,6</scenarios>
</agent>
<agent id="1">
<aname>pi1</aname>
<alive>0</alive>
<scenarios>1,2,3</scenarios>
</agent>
</agents>
This is not very reliable.
Thanks for any idea!
Try
$str = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<agents>
<agent id="1">
<aname>pi1</aname>
<alive>0</alive>
<scenarios>1,2,3</scenarios>
</agent>
<agent id="2">
<aname>pi2</aname>
<alive>1</alive>
<scenarios>4,5,6</scenarios>
</agent>
</agents>
XML;
$xml = new SimpleXMLElement($str);
foreach($xml->agent as $agent)
{
var_dump($agent);
echo "<hr>";
}

How to get value of xml element

I need to retrieve the value of the value of "TotalBooks" from an xml file that is structured like the example below.
I can get the equivalent of the "MatchesFound" value by doing a count of "book" and I can successfully get the information for each book.
However, I cannot get the actual value shown in the xml file for "MatchesFound", "TotalBooks", and "Page".
I'm using php with simplexml_load_file. Any help I can get is appreciated. Thanks.
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<MatchesFound>2</MatchesFound>
<TotalBooks>563</TotalBooks>
<Page>1</Page>
<book>
<title>Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price currency="USD">30.00</price>
</book>
<book>
<title>Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price currency="USD">29.99</price>
</book>
</bookstore>
$xml = new SimpleXMLElement($xmlString);
echo $xml->TotalBooks;
Using xpath :
<?php
$string = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<MatchesFound>2</MatchesFound>
<TotalBooks>563</TotalBooks>
<Page>1</Page>
<book>
<title>Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price currency="USD">30.00</price>
</book>
<book>
<title>Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price currency="USD">29.99</price>
</book>
</bookstore>
XML;
$xml = new SimpleXMLElement($string);
$result = $xml->xpath('//TotalBooks');
while(list( , $node) = each($result)) {
echo "$node\n";
}
?>
See http://php.net/manual/en/simplexmlelement.xpath.php

Have XML results in plaintext, want to loop through them in PHP

So I am working with an API that returns results in XML. Let's just say for argument sake I am returned the following:
<?xml version="1.0" encoding="UTF-8"?>
<Properties>
<Property>
<Name>Joes Crab Shack</Name>
<Address>111 Shack Street</Address>
</Property>
<Property>
<Name>Johns Shoe Store</Name>
<Address>123 Shoe Avenue</Address>
</Property>
</Properties>
Now I am using PHP and I get the results into a variable. So essentially this happens:
$xml_results = '<?xml version="1.0" encoding="UTF-8"?><Properties><Property><Name>Joes Crab Shack</Name><Address>111 Shack Street</Address></Property><Property><Name>Johns Shoe Store</Name><Address>123 Shoe Avenue</Address></Property></Properties>';
Now how can I treat this as an XML document and for example loop through it and print out all property names?
Something like this should get the job done.
$request_xml = '<?xml version="1.0" encoding="UTF-8"?>
<Properties>
<Property>
<Name>Joes Crab Shack</Name>
<Address>111 Shack Street</Address>
</Property>
<Property>
<Name>Johns Shoe Store</Name>
<Address>123 Shoe Avenue</Address>
</Property>
</Properties>';
$xml = simplexml_load_string($request_xml);
$i = 0;
while ($xml->Property[$i])
{
echo $xml->Property[$i]->Name;
echo $xml->Property[$i]->Address;
$i++;
}
Deserialize into an xml tree, try SimpleXML. That way you can access that data in a more convenient fashion and grab specific xml elements..

Categories