After calling the prestashop webservice, I recieved the response as shown below:
SimpleXMLElement Object
(
[order] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => 1
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => 2
)
)
)
)
I tried to retrive the content by looping as shown below:
foreach($resources as $resource){
echo '<pre>';
print_r($resource['id']);
echo '</pre>';
}
This gives me:
SimpleXMLElement Object
(
[0] => 1
)
SimpleXMLElement Object
(
[0] => 2
)
How can I retrieve 1 and 2 which are the values of these objects? thanks
I hate simplexml...
<?php
$xml = file_get_contents('xml.xml');
$xml = new SimpleXMLElement($xml);
foreach($xml as $key => $value)
{
$attrs = $value->attributes();
foreach($attrs as $attr_k => $attr_v)
echo $attr_k.": ".$attr_v."\n";
}
Related
againAnotherChild is an object now how can i put the data from it to an array.
foreach($anotherChild->children() as $againAnotherChild) //child to
//childchildchild
{
// echo "Inside Again child Tag attributes<br>";
$againAnotherChildArray[] = $againAnotherChild->getName();
//print_r($againAnotherChild);
// foreach($this->$againAnotherChild[0] as $Storage)
// {
// $store = $Storage;
// //echo $store;
// }
echo $againAnotherChild[0]."<br>";
//echo "Storage".$store;
}
if i do print_r($againAnotherChild) this is what i get which updates after each iteration
SimpleXMLElement Object ( [0] => uint8 ) SimpleXMLElement Object (
[0] => uint8 ) SimpleXMLElement Object ( [0] => enum )
SimpleXMLElement Object ( [0] => uint8 ) SimpleXMLElement Object ( [0]
=> enum ) SimpleXMLElement Object ( [0] => uint8 ) SimpleXMLElement Object ( [0] => firmware ) SimpleXMLElement Object ( [0] => enum )
SimpleXMLElement Object ( [0] => enum ) SimpleXMLElement Object ( [0]
=> uint8 ) SimpleXMLElement Object ( [0] => enum )
------------------------Next Iteration--------------
SimpleXMLElement Object ( [0] => nodeid ) SimpleXMLElement Object (
[0] => uint8 ) SimpleXMLElement Object ( [0] => ipaddress )
SimpleXMLElement Object ( [0] => macaddress ) SimpleXMLElement Object
( [0] => enum ) SimpleXMLElement Object ( [0] => uint8 )
SimpleXMLElement Object ( [0] => uint16 ) SimpleXMLElement Object (
[0] => uint16 ) SimpleXMLElement Object ( [0] => uint16 )
SimpleXMLElement Object ( [0] => uint16 ) SimpleXMLElement Object (
[0] => enum ) SimpleXMLElement Object ( [0] => enum ) SimpleXMLElement
Object ( [0] => uint16 ) SimpleXMLElement Object ( [0] => uint16 )
SimpleXMLElement Object ( [0] => uint16 ) SimpleXMLElement Object (
[0] => uint16 ) SimpleXMLElement Object ( [0] => enum )
How can i put these uint8,uint16 etc into an array that keeps updating till last iteration?
The simple solution was just to type cast an object to certain data type
$store = (array)againAnotherChildArray;
What i did in my code is this
$nodesWithValues = (array)$anotherChild->children();
foreach ($nodesWithValues as $key => $value)
{
//echo "$key : $value <br>";//CfgVer : uint8
var_dump($nodesWithValues);
}
problem solved by simple type casting :D
Are you asking about removing duplicates in array?
Would this help?
$againAnotherChildArray[$againAnotherChild->getName()] = $againAnotherChild->getName();
That should create something like hash map, eg set of tuples;
I want to parse XML string using SimpleXML on PHP.
$xml = '<root><case1><name> </name></case1><case2><name></name></case2><case3><name>ukits</name></case3></root>';
libxml_use_internal_errors(true);
$content = simplexml_load_string($xml);
if ($content === false) {
libxml_clear_errors();
}
print_r($content);
$content = objectToArray($content);
print_r($content);
function objectToArray($obj) {
if (is_object($obj)) { $obj = (array) $obj; }
if (count($obj) == 0) { $obj = ''; }
if (is_array($obj)) {
$new = array();
foreach ($obj as $key => $val) {
$new[$key] = objectToArray($val);
}
} else {
$new = $obj;
}
return $new;
}
This code returns:
SimpleXMLElement Object
(
[case1] => SimpleXMLElement Object
(
[name] => SimpleXMLElement Object
(
[0] =>
)
)
[case2] => SimpleXMLElement Object
(
[0] => SimpleXMLElement Object
(
)
)
[case3] => SimpleXMLElement Object
(
[name] => ukits
)
)
Array
(
[case1] => Array
(
[name] => Array
(
[0] =>
)
)
[case2] => Array
(
[0] =>
)
[case3] => Array
(
[name] => ukits
)
)
There seems to be 2 unexpected problems. In case1, name tag has a OBJECT child (not spaced string). In case2, case2 tag has a numeric array.
But, if the XML string contains line break or space after 'name' tag, the result is different. like this:
$xml = '<root><case1><name> </name>
</case1><case2><name></name>
</case2><case3><name>ukits</name></case3></root>';
The same code returns:
SimpleXMLElement Object
(
[case1] => SimpleXMLElement Object
(
[name] => SimpleXMLElement Object
(
)
)
[case2] => SimpleXMLElement Object
(
[name] => SimpleXMLElement Object
(
)
)
[case3] => SimpleXMLElement Object
(
[name] => ukits
)
)
Array
(
[case1] => Array
(
[name] =>
)
[case2] => Array
(
[name] =>
)
[case3] => Array
(
[name] => ukits
)
)
This result is that I want to get.
The point is WHY the results of "same structured XML strings" are differnt depend on line break.
And how do I do If I want to get what I want like second result?
(An input string have to contain no line break.)
Here is my configuration:
CentOS 6.7
PHP 5.4.45
SimpleXML Revision $Id: 16070fc92ad6f69cebb2d52ad3f02794f833ce39 $
libxml2 Version 2.7.6
AI have an XML which have attributes as well as values in them. I want to convert it to an Array or Array Object along with attributes and values.
XML
<?xml version="1.0" encoding="UTF-8"?>
<itemBody>
<div label="options">
<optionchoices optionIdentifier="RESPONSE" shuffle="false" maxOptions="1">
<choice identifier="A1"><![CDATA[aaaa]]></choice>
<choice identifier="A2"><![CDATA[bbbb]]></choice>
<choice identifier="A3"><![CDATA[cccc]]></choice>
</optionchoices>
</div>
</itemBody>
I tried two set of code but the result was not as expected.
Code 1
<?php
$xml = simplexml_load_file('test.xml', 'SimpleXMLElement', LIBXML_NOCDATA);
echo "<pre>";print_r($xml);echo "</pre>"; exit;
?>
Output
SimpleXMLElement Object
(
[div] => SimpleXMLElement Object
(
[#attributes] => Array
(
[label] => options
)
[optionchoices] => SimpleXMLElement Object
(
[#attributes] => Array
(
[optionIdentifier] => RESPONSE
[shuffle] => false
[maxOptions] => 1
)
[choice] => Array
(
[0] => aaaa
[1] => bbbb
[2] => cccc
)
)
)
)
In the above output if we check then in choice node we get the values only and not the attributes
Code 2
<?php
$xml = simplexml_load_file('test.xml');
echo "<pre>";print_r($xml);echo "</pre>"; exit;
?>
Output
SimpleXMLElement Object
(
[div] => SimpleXMLElement Object
(
[#attributes] => Array
(
[label] => options
)
[optionchoices] => SimpleXMLElement Object
(
[#attributes] => Array
(
[optionIdentifier] => RESPONSE
[shuffle] => false
[maxOptions] => 1
)
[choice] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => A1
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => A2
)
)
[2] => SimpleXMLElement Object
(
[#attributes] => Array
(
[identifier] => A3
)
)
)
)
)
)
In this output we get only attributes of XML.
Now what I want is to obtain Attributes as well as Values of the XML.
Please help.
Thanks in advance.
This is what I got. And this is the solution which I expected.
http://outlandish.com/blog/xml-to-json/
I am using php to consume a web service (in coldfusion) to validate against the active directory. My code is below.
<?php
$racf = $_SERVER['AUTH_USER'];
//echo $racf;
//echo $myracf = trim($racf, "FEDERATED\.");
//get authenticated user
$arrUser = explode("\\", $_SERVER["LOGON_USER"]);
$racf = $arrUser[1];
echo $racf.'<br ><br >';
$logins = "http://acoldfusionwebservice/login.cfc?method=loginad&racf=$racf";
if( ! $xml = simplexml_load_file($logins) )
{
echo 'unable to load XML file';
}
else
{
//echo 'XML file loaded successfully <br />';
print_r ($xml);
}
?>
And this produces the following.
SimpleXMLElement Object
(
[#attributes] => Array
(
[version] => 1.0
)
[header] => SimpleXMLElement Object
(
)
[data] => SimpleXMLElement Object
(
[recordset] => SimpleXMLElement Object
(
[#attributes] => Array
(
[rowCount] => 1
[fieldNames] => cn,mail,givenName,sn
)
[field] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => cn
)
[string] => B000000
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => mail
)
[string] => john.doe#company.com
)
[2] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => givenName
)
[string] => John
)
[3] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => sn
)
[string] => Doe
)
)
)
)
)
Can someone help me to parse this information so that I can assign variables and use them. Thanks.
Try
$xml->data->recordset->field[0]->string
I think that field[0] is an Object again
I am parsing the array given below. Looping through td using foreach. Now i want to select the value other than [#attributes]. I cannot use a or p specifically as they change through out the objects.
How can i achieve this?
[0] => SimpleXMLElement Object
(
[th] => SimpleXMLElement Object
(
[#attributes] => Array
(
[rowspan] => 2
[scope] => row
)
[p] => Memory
)
[td] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[class] => ttl
)
[a] => Card slot
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[class] => nfo
)
[p] => No
)
)
)
Want the solution to work in php.
Try below one
<?php
foreach($td as $element)
{
foreach($element as $key => $value)
{
if(!preg_match("/#/", $key) && !is_array($value))
echo $element[$key];
}
}
?>