<user>
<name>
<dob><test>15</test> </dob>
<age></age>
</name>
<name1><test></test></name1>
<name2></name2>
</user>
Explain : i have need output like(
user.name.dob
user.name.age
user.name1.test
user.name2) this.i want use one for each .Can i get by recursion method.if yes that how.please help me out this
Here's a simple example, you'll want to restructure XML a bit.
$xml = new SimpleXMLElement('<parent><user>
<name>John</name>
<dob>1/1/2015</dob>
<age>12</age>
</user>
<user>
<name>luke</name>
<dob>2/1/2015</dob>
<age>21</age>
</user></parent>');
$base = 0;
foreach ($xml as $user) {
$base++;
echo $base . '.user.' . $user->name . '.' . $user->dob . '.' . $user->age . "\n";
}
Link to the manual, http://php.net/manual/en/simplexmlelement.children.php.
Related
How to read ALL atribute xml:lang values?
Sometimes I do not know how many languages are defined in XMLs data.
<?xml version="1.0" encoding="UTF-8"?>
<offer>
<products>
<product>
<description>
<name xml:lang="eng">English translation</name>
<name xml:lang="lat">Latvian translation</name>
</description>
</product>
<product>
<description>
<name xml:lang="eng">The same English</name>
<name xml:lang="pol">And Polish language</name>
</description>
</product>
</products>
</offer>
I can xml:lang parse in PHP by adding exact language code in xpath
print_r($xml->xpath('products/product/description/name[#xml:lang = "eng"]'));
But I need to add all xml:lang atributes values to parsed array.
Can it be done with PHP SimpleXML?
what about this:
$nodes = $xml->xpath('products/product/description/name[#xml:lang]');
Will return an array of <name>-nodes.
If this is not it, please clarify exactly your desired result.
EDIT
try this to get the xml:lang attributes only:
$langs = $xml->xpath("products/product/description/name[#xml:lang]/#xml:lang");
// $lang is an array of simplexml-elements, transform the values to string like this:
$langs = array_map("strval", $langs);
I'm not 100% on SimpleXML sorry, but I know DomDocument can do what you are after. Hopefully this can be of use to you:
$xmlstring = '<?xml version="1.0" encoding="UTF-8"?>
<offer>
<products>
<product>
<description>
<name xml:lang="eng">English translation</name>
<name xml:lang="lat">Latvian translation</name>
</description>
</product>
<product>
<description>
<name xml:lang="eng">The same English</name>
<name xml:lang="pol">And Polish language</name>
</description>
</product>
</products>
</offer>';
$dom = new DOMDocument();
$dom->loadXML($xmlstring); //or $dom->load('filename.xml');
$xpath = new DOMXPath($dom);
$nodes = $xpath->query('//products/product/description/name');
foreach ($nodes as $node) {
echo 'Language: ' . $node->getAttribute('xml:lang') . '<br />';
echo 'Value: ' . $node->nodeValue . '<br /><br />';
}
You can assign $node->getAttribute('xml:lang') to a variable and run some checks to see if it matches 'eng' or whatever you need.
I used xpath as you had in your original post, but you can also use $dom->getElementsByTagName('name') and access values and attributes in much the same way.
I found easier way to access namespaced attributes. You could use $name->attributes("xml", true) function.
Here is working example:
<?php
$xmlString = '
<products>
<product>
<name xml:lang="eng">
Apples
</name>
<name xml:lang="fr">
Pommes
</name>
</product>
<product>
<name xml:lang="eng">
Strawberries
</name>
<name xml:lang="fr">
Fraises
</name>
</product>
</products>
';
$xml = new \SimpleXMLElement($xmlString);
foreach($xml->product as $product)
{
foreach ($product->name as $name)
{
$attributes = $name->attributes("xml", true);
// print_r($attributes);
foreach ($attributes as $attributeName => $attributeValue)
{
// echo $attributeName . PHP_EOL;
// echo $attributeValue . PHP_EOL;
if ($attributeValue == "eng" && $attributeName == "lang") {
echo "English: " . trim(((string) $name)) . PHP_EOL;
}
if ($attributeValue == "fr" && $attributeName == "lang") {
echo "French: " . trim(((string) $name)) . PHP_EOL;
}
}
}
}
Online demo: https://repl.it/repls/LovingFineDaemon
I have an XML file that looks like this:
<?xml version="1.0" encoding="ISO-8859-1"?>
<products>
<product sku="CATDJ" type="CAT" vendor="DJ" active="1" on_sale="0" discountable="0">
<name>CATALOGS</name>
<short_description><![CDATA[The DJ catalog features 182 pages]]></short_description>
<long_description><![CDATA[The DJ catalog features 182 pages.]]></long_description>
<price>1.5</price>
<stock_quantity>65</stock_quantity>
<release_date>2003-05-06T00:00:00-04:00</release_date>
<barcode>782421791315</barcode>
</product>
....
I can get the price, stock_quatity, and barcode, but not the sku, active or discountable data.
This is what my code looks like this:
$myinv = simplexml_load_file('http://www.*******.com/products.xml');
foreach ($myinv as $invinfo):
$sku = $invinfo->products->product->sku;
$active = $invinfo->products->product->active;
$deductible = $invinfo->products->product->discountable;
$qty=$invinfo->stock_quantity;
$price=$invinfo->price;
$upc=$invinfo->barcode;
What Am I doing wrong? BTW, I'm new to php.
Thank you for your help.
I always typecast everything I get from SimpleXML, because it always returns a SimpleXMLElement. var_dump your variables to see for yourself.
<?php
$xml = '<?xml version="1.0" encoding="ISO-8859-1"?>
<products>
<product sku="CATDJ" type="CAT" vendor="DJ" active="1" on_sale="0" discountable="0">
<name>CATALOGS</name>
<short_description><![CDATA[The DJ catalog features 182 pages]]></short_description>
<long_description><![CDATA[The DJ catalog features 182 pages.]]></long_description>
<price>1.5</price>
<stock_quantity>65</stock_quantity>
<release_date>2003-05-06T00:00:00-04:00</release_date>
<barcode>782421791315</barcode>
</product>
</products>';
$myinv = new SimpleXMLElement($xml);
$products = $myinv->product;
foreach ($products as $product){
$attrs = $product->attributes();
$sku = $attrs->sku;
$active = $attrs->active;
$discountable = $attrs->discountable;
$qty = $product->stock_quantity;
$price = $product->price;
$upc = $product->barcode;
echo (string)$sku . "<br>\n";
echo (string)$active . "<br>\n";
echo (string)$discountable . "<br>\n";
echo (string)$qty . "<br>\n";
echo (string)$price . "<br>\n";
echo (string)$upc . "<br>\n";
}
?>
XPath can do the job of getting the value of an attribute with $xpath->query("//product[#name='sku']/#value");.
I am trying to edit some XML with PHP. Currently the XML looking something like:
<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0">
<channel>
<title>Main Title</title>
<link>http://exmaple.com</link>
<description> blahblahblah </description>
<language>en</language>
<item>
<title>Tite1</title>
<link>http://www.example.com (THIS IS WHAT I WANT)</link>
<description>blah blah blah</description>
</item>
.
.
.
</channel>
</rss>
I've tried to access the 2nd level link but my code only changes the first Link node value. Here is the code:
$xml->load('http://www.google.com/doodles/doodles.xml');
$element = $xml->getElementsByTagName('channel')->item(0);
$secondlvl = $element->getElementsByTagName('item')->item(0);
$2ndlevellinknode = $element->getElementsByTagName('link')->item(0);
$2ndlevellinknode->nodeValue = $newvalue;
Any suggestions? Also is it possible to use this line of code in a for loop like this
for ($i = 0; $i <= 20; $i++) {
$element = $xml->getElementsByTagName('channel')->item(0);
$secondlvl = $element->getElementsByTagName('item')->item(0);
$2ndlevellinknode = $element->getElementsByTagName('link')->item($i);
$2ndlevellinknode->nodeValue = $newvalue;
}
this should give you an idea.
$f = simplexml_load_file('test.xml');
print $f->channel->title . "\n";
print $f->channel->link . "\n";
print $f->channel->description . "\n";
foreach($f->channel->item as $item) {
print $item->title . "\n";
}
Im trying to loop through and display info from the following xml structure.
<users_list>
−<users type="array">
+<user>
<id>Blah</id>
</user>
+<user></user>
+<user></user>
</users>
<next_link>6</next_link>
<prev_link>4</prev_link>
</users_list>
Im using the following PHP to grab the nodes.
$xml = simplexml_load_string($rawxml);
foreach($xml->users_list AS $key){
$name = $key->users->user->{"id"};
}
$next = $key->{"next_link"};
$prev = $key->{"prev_link"};
Ive tried a couple variations, but i dont see any effect. I either get nothing when i echo my variables, or invalid arguments when on my foreach function
When using SimpleXML, you should always name your variables after the root node they contain, it makes things simpler and obvious:
$users_list = simplexml_load_string(
'<users_list>
<users type="array">
<user>
<id>Blah</id>
</user>
<user></user>
<user></user>
</users>
<next_link>6</next_link>
<prev_link>4</prev_link>
</users_list>'
);
foreach ($users_list->users->user as $user)
{
echo "User ", $user->id, "\n";
}
echo "next: ", $users_list->next_link, "\n";
echo "prev: ", $users_list->prev_link, "\n";
When troubleshooting in PHP, var_dump and print_r are your friend!
If you wish to browse your result like an array, then cast it to an array.
$value = (array) $value;
I did the following:
$xmlStr = '<users_list>
<users type="array">
<user>
<id>Blah</id>
</user>
<user></user>
<user></user>
</users>
<next_link>6</next_link>
<prev_link>4</prev_link>
</users_list>';
$xml = simplexml_load_string($xmlStr);
foreach($xml->users->user AS $key=>$value){
$value = (array) $value;
$name = $value["id"];
var_dump($name);
}
which gives the output:
string(4) "Blah"
NULL
NULL
Check the PHP help documents for further info on simplexml
http://nl2.php.net/manual/en/function.simplexml-load-string.php
http://nl2.php.net/manual/en/class.simplexmlelement.php
print_r($xml) should give you all the information you need.
You will probably find that the actual array is $xml->user_list->users->user,
also casting helps save some time
foreach($xml->user_list->users->user as $value) {
$name = (string) $value->id;
}
Here is my PHP code
$xml= new SimpleXmlElement($rawxml);
foreach($xml->children()->children() AS $key){
$id = $xml->{"id"};
$name = $xml->{"screen_name"};
$profimg = $xml->{"profile_image_url"};
echo "$id, $name, $profimg";
}
$next = $xml->{"next_link"};
echo "index.php?".$next;
Here is the structure of my xml
<?xml version="1.0" encoding="UTF-8"?>
<users_list>
<users type="array">
<user>
<id>44444</id>
<screen_name>Some Name</screen_name>
<profile_image_url>http://www.website.com/picture.jpg</profile_image_url>
</user>
<user>
<id>555</id>
<screen_name>Bob</screen_name>
<profile_image_url>http://www.website.com/picture2.jpg</profile_image_url>
</user>
<user>
<id>666666</id>
<screen_name>Frank</screen_name>
<profile_image_url>http://www.website.com/picture3.jpg</profile_image_url>
</user>
</users>
<next_link>44444</next_link>
</users_list>
Im trying to assign the values of the field to variables and then echo them. Then at the bottom echo the nextlink.
I dont get any errors, but it just shows the first field over and over, and doesnt output the nextlink.
You’re using $key in the foreach expression but $xml inside the foreach body.
I would also prefer an XPath expression rather than your children of the children of the root way:
foreach ($xml->xpath('/users_list/users/user') as $user) {
$id = $user->id;
$name = $user->screen_name;
$profimg = $user->profile_image_url;
echo "$id, $name, $profimg";
}