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";
}
Related
Hi i want to create a php code which generates xml of specific format given below. Kindly help me through this
<?xml version="1.0" encoding="utf-8"?>
<order>
<requisition>
<dateofservice>2012-12-13</dateofservice>
<labid>str1234</labid>
<reqnotes_c>str1234</reqnotes_c>
<rptemail_c>str1234</rptemail_c>
<rptmethod_c>str1234</rptmethod_c>
<specimen_type_c>str1234</specimen_type_c>
<collectiontype_c>str1234</collectiontype_c>
</requisition>
<patient>
<firstname>str1234</firstname>
<lastname>str1234</lastname>
<dob>2012-12-13</dob>
<breed_c>str1234</breed_c>
<species>str1234</species>
<sex>s</sex>
<sterilization_c>str1234</sterilization_c>
</patient>
</order>
My code is this Now correct me please. My code gives only one parent and i want to generate the output as given above
while (($row = fgetcsv($inputFile)) !== FALSE){
$container = $doc->createElement('row');
foreach($headers as $i => $header) {
if(!empty($row[$i])) {
$child = $doc->createElement($header);
$child = $container->appendChild($child);
$value = $doc->createTextNode($row[$i]);
$value = $child->appendChild($value);
}
}
$root->appendChild($container);
}
$strxml = $doc->saveXML();
<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.
I received a very weird XML file to process.
Instead of grouping names with group tags, like this:
<data>
<group>
<name>John</name>
<name>Mary</name>
<name>Susan</name>
</group>
<group>
<name>Cesar</name>
<name>Joseph</name>
<name>Sylvia</name>
<name>Steve</name>
</group>
</data>
It inserts a separator tag after each element, like this:
<data>
<name>John</name>
<separator>,</separator>
<name>Mary</name>
<separator>,</separator>
<name>Susan</name>
<separator>;</separator>
<name>Cesar</name>
<separator>,</separator>
<name>Joseph</name>
<separator>,</separator>
<name>Sylvia</name>
<separator>,</separator>
<name>Steve</name>
<separator>.</separator>
</data>
";" and "." are the group delimiters. (I know, that's weird, but I can't change that and I have to process a lot of these files)
To get all names and all separators I can use the following code:
$data = <<<XML
<data>
<name>John</name>
<separator>,</separator>
<name>Mary</name>
<separator>,</separator>
<name>Susan</name>
<separator>;</separator>
<name>Cesar</name>
<separator>,</separator>
<name>Joseph</name>
<separator>,</separator>
<name>Sylvia</name>
<separator>,</separator>
<name>Steve</name>
<separator>.</separator>
</data>
XML;
$xml = simplexml_load_string($data);
foreach ($xml->name as $name){
echo "$name\n";
}
foreach ($xml->separator as $sep){
echo "$sep\n";
}
But this way, I can't get the name and the correspondent separator on a single loop.
Is there any way to know, on the first loop, the next element of each name?
I hope that i understand your question.
$xml = simplexml_load_string($data);
$i = 0;
$res = '';
foreach ($xml->name as $name){
$res .= "$name ".$xml->separator[$i];
$i++;
}
$groups = explode(';',$res);
Parse the XML like this and build an array:
$xml = simplexml_load_string($x);
foreach ($xml->children() as $name => $value) {
if ($name == 'name') $names[$i][] = (string)$value;
elseif ($name == 'separator' && $value == ';') $i++;
}
Output $names:
array(2) {
[0]=>array(3) {
[0]=>string(4) "John"
[1]=>string(4) "Mary"
[2]=>string(5) "Susan"
}
[1]=>array(4) {
[0]=>string(5) "Cesar"
[1]=>string(6) "Joseph"
[2]=>string(6) "Sylvia"
[3]=>string(5) "Steve"
}
}
You can then just pick the names from the array.
see it working: https://eval.in/95853
I have a soap xml that contains a bunch of variables that I need to access. Here is the XML.
`<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<searchPersonsResponse xmlns="">
<searchPersonsReturn>
<attributes>
<attributes>
<name>ercreatedate</name>
<values>
<values>201104070130Z</values>
</values>
</attributes>
<attributes>
<name>status</name>
<values>
<values>Stuff1</values>
<values>Stuff2</values>
<values>Stuff3</values>
<values>Stuff4</values>
<values>Stuff5</values>
<values>Stuff6</values>
<values>Stuff7</values>
</values>
</attributes>
</attributes>
<itimDN>blah</itimDN>
<name>Smith, Bob</name>
<profileName>PER</profileName>
<select>false</select>
</searchPersonsReturn>
</searchPersonsResponse>
</soapenv:Body>
</soapenv:Envelope>
I'm trying to access the inner attribute node and pull out the Name and values into a multidimentional array like this ....
$array["status"][0]="stuff1";
$array["status"][1]="stuff2";
$array["status"][2]="stuff3";
$array["status"][3]="stuff4";
so far I have been able to access the nodes but not really get them the way I want. here is the code I have been playing around with .....
$dom_document = new DOMDocument();
$dom_document->loadXML($thexml);
$tag_els_names = $dom_document->getElementsByTagname('name');
$tag_els_values = $dom_document->getElementsByTagname('values');
$data = array();
$data2 = array();
foreach($tag_els_names as $node){
$data[] = array($node->nodeName => $node->nodeValue);
//grabs all the <name> node values
}
$i=0;$j=0;
foreach($tag_els_values as $node){
$j=0;
foreach($node->childNodes as $child) {
$data2[$i][$j] = $child->nodeValue;
//grabs all the value node values
$j++;
}
$i++;
$j=0;
}
Does anyone know an easy way to do this? I think that I have been looking at this for way to long.
How about something like:
$dom_document = new DOMDocument();
$dom_document->loadXML($thexml);
$xpath = new DOMXpath($dom_document);
$attr = $xpath->evaluate("//attribute/attributes");
$names = array();
$values = array();
$i = 0;
foreach($attr as $attr_node) {
$values[i] = array();
foreach($xpath->evaluate("name", $attr_node) as $name){
$names[] = $name->nodeValue;
}
$foreach($xpath->evaluate("value", $attr_node) as $value){
$values[i][] = $value->nodeValue;
}
i++;
}
This would, however, miss the <name> element that's outside of the <attributes> group. Did you mean to be including that?
I figured this out and thought it could help someone else
$doc = new DOMDocument();
$values=array();
if ($doc->loadXML($temp)) {
$attributes = $doc->getElementsByTagName('attributes');
foreach($attributes as $attribute) {
if($attribute->childNodes->length) {
$previous_nodeValue="";
foreach($attribute->childNodes as $i) {
if($i->nodeValue=="status"){
$previous_nodeValue=$i->nodeValue;
}
if($i->nodeName=="values" && $previous_nodeValue== "status"){
foreach($i->childNodes as $j){
$values[]=$j->nodeValue;
}
}
}
$previous_nodeValue="";
}
}
}
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;
}