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;
}
Related
My XML looks like this:
<n10:category xmlns:n10="..." some-id="123">
<n10:name xml:lang="x-default">Name Here</n10:name>
<n10:custom-attributes>
<n10:custom-attribute attribute-id="abc1">1</n10:custom-attribute>
<n10:custom-attribute attribute-id="abc2">false</n10:custom-attribute>
<n10:custom-attribute attribute-id="abc3">false</n10:custom-attribute>
...
To access some-id I call:
$xml = simplexml_load_string(...);
foreach ($xml->attributes() as $key => $value) {
if ($key == 'some-id') {
$data['some_id'] = (string) $value;
}
}
The above works. However, when I try to access attributes of custom-attribute, I get back values (like 1, false, false in the example above, but I am unable to get what the attribute-id is equal to on each record. I've tried:
foreach ... $xml->{'custom-attributes'}->attributes() and it returns null
Also, doing var_dump of $xml in the beginning doesn't seem to include the attribute-id at all.
What am I missing?
Use the xpath method to access the custom-attribute nodes, like this:
<?php
$xml = <<<XML
<?xml version='1.0' standalone='yes'?>
<n10:category xmlns:n10="..." some-id="123">
<n10:name xml:lang="x-default">Name Here</n10:name>
<n10:custom-attributes>
<n10:custom-attribute attribute-id="abc1">1</n10:custom-attribute>
<n10:custom-attribute attribute-id="abc2">false</n10:custom-attribute>
<n10:custom-attribute attribute-id="abc3">false</n10:custom-attribute>
</n10:custom-attributes>
</n10:category>
XML;
$sx = simplexml_load_string($xml);
$sx->registerXPathNamespace('n10', '...');
$customAttributes = $sx->xpath('/n10:category//n10:custom-attribute');
foreach ($customAttributes as $ca) {
echo $ca['attribute-id'] . '<br>';
}
It's important to register the custom namespace to be able to access the nodes belonging to said namespace.
I have following xml getting loaded in my PHP code;
<SiteAlarmDetails>
<AlertId>89637</AlertId>
<SiteCode>20157498</SiteCode>
<SiteName>newport</SiteName>
</SiteAlarmDetails>
$alertXml = simplexml_load_string( $tableAlarm->AlarmDetails);
echo (string) $alertXml->AlertId; //prints **89637**
Now I try to traverse this XML nodes;
foreach($alertXml->children() as $alerts)
{
$alertId = (string)$alerts->AlertId;
echo $alertId;//I do not see anything
}
Is above right approach to traverse AlertId in the foreach loop?
Trying simple foreach will be helpful. Just for accessing single value (eg AlertId) you can use $alertXml->AlertId;.
Try this code snippet here
<?php
ini_set('display_errors', 1);
$xmlString=<<<XML
<SiteAlarmDetails>
<AlertId>89637</AlertId>
<SiteCode>20157498</SiteCode>
<SiteName>newport</SiteName>
</SiteAlarmDetails>
XML;
$alertXml = simplexml_load_string( $xmlString);
foreach($alertXml as $key => $child)
{
echo $key ."=".(string)$alertXml->{$key};
echo PHP_EOL;
}
Output:
AlertId=89637
SiteCode=20157498
SiteName=newport
I have the following example XML:
<PRODUCTRATINGLIST>
<PRODUCT>
<VENDORREF>AC308A~</VENDORREF>
<RATING>100%</RATING>
<REVIEWCOUNT>7</REVIEWCOUNT>
</PRODUCT>
<PRODUCT>
<VENDORREF>AC308C~</VENDORREF>
<RATING>98%</RATING>
<REVIEWCOUNT>89</REVIEWCOUNT>
</PRODUCT>
</PRODUCTRATINGLIST>
I'm simply trying to extract each node under PRODUCT:
$ratings = simplexml_load_file("test.xml");
foreach ($ratings->PRODUCT as $rating){
$part = $rating->VENDORREF;
$rating = str_replace('%','',$rating->RATING);
$numReviews = $rating->REVIEWCOUNT;
}
If I then try to print each element e.g.
echo $part.' '.$rating.' '.$numReviews;
$numReviews is always blank and I have no idea why.
You are replacing the $rating array with a variable, fix it like this:
$part = $rating->VENDORREF;
$rating_string = str_replace('%','',$rating->RATING);
$numReviews = $rating->REVIEWCOUNT;
Check below code. You change the variable names.
$ratings = simplexml_load_file("test.xml");
foreach ($ratings->PRODUCT as $rating){
$part = $rating->VENDORREF;
$ratingVal = str_replace('%','',$rating->RATING);
$numReviews = $rating->REVIEWCOUNT;
}
echo $part.' '.$ratingVal.' '.$numReviews;
I get response from SOAP server which has zero or more transactions of different types in each response.
Each transaction type is extension of base transaction type.
Different transaction types are processed differently.
Is there a way in PHP to get transaction type for each of transactions in response
(other then trying to figure difference in elements within each complex type)?
There is lot of types and lot of elements in each type....
Is there any class which could get this?
Following is just illustration...
<transactions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:type1">
<id>24111</id><something>00000000</something><name>Blah</name>
</transactions>
<transactions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:type8">
<id>24111</id><somethingelse>011</somethingelse>
</transactions>
I 'm not quite sure if this answer fits your question exactly. The following code snippet gets the type attribute value by their given namespaces and not the type of the namespaced value itself.
Done with PHP 's own Document Object Model.
<?php
$str = <<<XML
<content>
<transactions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:type1">
<id>24111</id>
<something>00000000</something>
<name>Blah</name>
</transactions>
<transactions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:type8">
<id>24111</id>
<somethingelse>011</somethingelse>
</transactions>
</content>
XML;
$doc = new DomDocument();
$doc->loadXML($str);
$nodeList = $doc->getElementsByTagName('transactions');
foreach ($nodeList as $element) {
$value = $element->getAttributeNS('http://www.w3.org/2001/XMLSchema-instance', 'type');
echo $value . "\n";
}
This will output the two given types "ns2:type1" and "ns2:type8".
I can parse your elements with simple_html_dom.
Here is the link for it.
An example is here :
<?php
include "simple_html_dom.php";
$html_nb = '
<transactions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:type1"><id>24111</id><something>00000000</something><name>Blah</name>
</transactions>
<transactions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:type8"><id>24111</id><somethingelse>011</somethingelse>
</transactions>';
function chtml($str){
if(strpos("<html>", $str) !== false)
return '<html><whole_code>'.$str.'</whole_code></html>';
else
return "<whole_code>".$str."</whole_code>";
}
function find_element_type($str){
if(preg_match_all("/\<(.*?)\>/i", $str, $matches))
return $matches[1][0];
else
return false;
}
function get_xsi_type($str){
if(preg_match_all("/xsi\:type\=\"(.*?)\"/i", $str, $matches))
return $matches[1][0];
else
return false;
}
$html = new simple_html_dom();
$html_2 = new simple_html_dom();
$html->load(chtml($html_nb));
$max_type = 10;
$element = $html->find('whole_code');
$e = $element[0]->innertext;
$html_2->load(chtml($e));
$k = 0;
while($html_2->find("whole_code",false)->children($k) != "")
{
$all = $html_2->find("whole_code",false)->children($k);
echo get_xsi_type($all) . "<br>";
echo find_element_type($all) . " : " .$all."<br>";
$k++;
}
echo "<hr>";
The result :
ns2:type1
transactions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:type1" : 2411100000000Blah
ns2:type8
transactions xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ns2:type8" : 24111011
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";
}