I'm currently using file_get_contents to get an xml.
It work well and when I display the xml with the correct MIME type header('Content-type: text/xml') I obtain something like this :
<?xml version="1.0" encoding="iso-8859-1" ?>
<tarification compagnie="banane" cle="laclef">
<gamme reference="equilibre-sante">
<tarif formule="f100">Xx.xx</tarif>
<tarif formule="f200">Xx.xx</tarif>
</gamme>
</tarification>
To use it as an object I use simplexml_load_string but when I print_r the returned object I didn't see the formule attribute I just see something like this :
SimpleXMLElement Object
(
[#attributes] => Array
(
[compagnie] => banane
[cle] => laclef
)
[gamme] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[reference] => equilibre-sante
)
[tarif] => Array
(
[0] => Xx.xx
[1] => Xx.xx
)
)
)
)
I want to get formule attributes, I have already tested to do this by following this tutorial without success.
You need to use the SimpleXMLElement::attributes as:
$xml = simplexml_load_string($xmlstring);
foreach($xml->gamme->tarif as $tarif) {
foreach($tarif->attributes() as $a => $b) {
echo $a,'="',$b,"\"\n";
}
}
See it
Related
Sorry if this seems like an easy question, but I've started pulling hair out on this...
I have a XML file which looks like this...
<VAR VarNum="90">
<option>1</option>
</VAR>
I'm trying to get the VarNum.
So far I've been successful using the follow code to get the other information:
$xml=simplexml_load_file($file);
$option=$xml->option;
I just can't get VarNum (the attribute value I think?)
Thanks!
You should be able to get this using SimpleXMLElement::attributes()
Try this:
$xml=simplexml_load_file($file);
foreach($xml->Var[0]->attributes() as $a => $b) {
echo $a,'="',$b,"\"\n";
}
That will show you all the name/value attributes for the first foo element. It's an associative array, so you can do this as well:
$attr = $xml->Var[0]->attributes();
echo $attr['VarNum'];
What about using $xml['VarNum'] ?
Like this :
$str = <<<XML
<VAR VarNum="90">
<option>1</option>
</VAR>
XML;
$xml=simplexml_load_string($str);
$option=$xml->option;
var_dump((string)$xml['VarNum']);
(I've used simplexml_load_string because I've pasted your XML into a string, instead of creating a file ; what you are doing with simplexml_load_file is fine, in your case !)
Will get you
string '90' (length=2)
With simpleXML, you access attributes with an array syntax.
And you have to cast to a string to get the value, and not and instance of SimpleXMLElement
For instance, see example #5 of Basic usage in the manual :-)
[0] => Array
(
[#attributes] => Array
(
[uri] => https://abcd.com:1234/abc/cst/2/
[id] => 2
)
[name] => Array
(
[first] => abcd
[last] => efg
)
[company] => abc SOLUTION
[email] => abc#xyz.com
[homepage] => WWW.abcxyz.COM
[phone_numbers] => Array
(
[phone_number] => Array
(
[0] => Array
(
[main] => true
[type] => work
[list_order] => 1
[number] => +919876543210
)
[1] => Array
(
[main] => false
[type] => mobile
[list_order] => 2
[number] => +919876543210
)
)
)
[photo] => Array
(
[#attributes] => Array
(
[uri] => https://abcd.com:1234/abc/cst/2/cust_photo/
)
)
)
I applied the below code
$xml = simplexml_load_string($response);
$json = json_encode($xml);
$array = json_decode($json,TRUE);
print_r($array);
but it is not use full i want all the data in single array in php
Try this
$xml->attributes()['YourPropertyName']; //check property case also
I have a XML file simillar to this :
<information version="2">
<currentTime>2014-06-06 17:28:16</currentTime>
<result>
<name>Mark</name>
<surname>Smith</surname>
</result>
I read it with php function and parse it to the object with function, like this:
function parse_data($data){
$return_data['currentTime'] = $data->currentTime;
$return_data['name'] = $data->result->name;
$return_data['surname'] = $data->result->surname;
return $return_data;
}
$xml = simplexml_load_string(file_get_contents($link));
$object = parse_data($xml);
Then, when I echo it on the screen, to check how it look:
//json_encode($xml);
{
"#attributes":{"version":"2"},
"currentTime":"2014-06-06 17:28:16",
"result":{"name":"Mark","surname":"Smith"}
}
//print_r($xml);
SimpleXMLElement Object (
[#attributes] => Array ( [version] => 2 )
[currentTime] => 2014-06-06 17:56:30
[result] => SimpleXMLElement Object (
[name] => Mark
[surname] => Smith
)
)
//json_encode($object);
{
"currentTime":{"0":"2014-06-06 17:28:16"},
"name":{"0":"Mark"},
"surname":{"0":"Smith"}
}
//print_r($object);
Array (
[currentTime] => SimpleXMLElement Object ( [0] => 2014-06-06 17:52:50 )
[name] => SimpleXMLElement Object ( [0] => Mark)
[surname] => SimpleXMLElement Object ( [0] => Smith )
)
What is wrong with my code? He seems to read the informaton in xml file as array? Because of this strange notation I cannont operate on this data normally.
It also behave like this:
echo json_encode($object['name']); will give -> {"0":"Mark"}
echo $object['name']; will give -> Mark
Can anybody help me? What am I doing wrong?
I want my $object to look like this:
//json_encode($object);
{
"currentTime":"2014-06-06 17:28:16",
"name":"Mark",
"surname":"Smith"
}
Edit1: added print_r values
Yes, as you have noticed the type returned by $someSimpleXMLNode is an object. If you want the node value (as a string for example) use:
$return_data['currentTime'] = (string)$data->currentTime;
which is the same as doing
$return_data['currentTime'] = $data->currentTime->__toString();
etc
When you do
echo $data->currentTime;
the node is automatically coerced into a string (because echo only handles strings). This is done (generally, in php) by the object's __toString method.
I retrieve the following XML data:
<?xml version="1.0" encoding="UTF-8"?>
<JMF xmlns="http://www.CIP4.org/JDFSchema_1_1" MaxVersion="1.4" SenderID="HP Hybrid Elk JMF" TimeStamp="2014-02-19T07:42:11+00:00" Version="1.4" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="JMFRootMessage">
<!--Generated by the CIP4 Java open source JDF Library version : CIP4 JDF Writer Java 1.4a BLD 63-->
<Response ID="Rgdhhhdfhd" ReturnCode="0" Type="KnownDevices" refID="gdhhhdfhd" xsi:type="ResponseKnownDevices">
<DeviceList>
<DeviceInfo DeviceCondition="OK" DeviceID="HPSSPP-SM" DeviceStatus="Running" StatusDetails="Running"/>
<DeviceInfo CounterUnit="Impressions" DeviceCondition="OK" DeviceID="192.168.1.101" DeviceStatus="Running" ProductionCounter="12345678" StatusDetails="Indigo: Printing" xmlns:jdf="http://www.CIP4.org/JDFSchema_1_1">
<GeneralID IDUsage="hpCustomerImpressionCounter" IDValue="12345678.0"/>
</DeviceInfo>
<DeviceInfo CounterUnit="Impressions" DeviceCondition="OK" DeviceID="192.168.1.102" DeviceStatus="Running" ProductionCounter="23456789" StatusDetails="Indigo: Printing" xmlns:jdf="http://www.CIP4.org/JDFSchema_1_1">
<GeneralID IDUsage="hpCustomerImpressionCounter" IDValue="23456789.0"/>
</DeviceInfo>
</DeviceList>
</Response>
</JMF>
I load it into a SimpleXMLElement:
<?php
$jdf_response = new SimpleXMLElement($xml_response);
And I can then display it like so:
<pre>
<?php print_r($jdf_response->Response->DeviceList); ?>
</pre>
Which gives the following output:
SimpleXMLElement Object
(
[DeviceInfo] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[DeviceCondition] => OK
[DeviceID] => HPSSPP-SM
[DeviceStatus] => Running
[StatusDetails] => Running
)
)
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[CounterUnit] => Impressions
[DeviceCondition] => OK
[DeviceID] => 192.168.1.101
[DeviceStatus] => Running
[ProductionCounter] => 12345678
[StatusDetails] => Indigo: Printing
)
[GeneralID] => SimpleXMLElement Object
(
[#attributes] => Array
(
[IDUsage] => hpCustomerImpressionCounter
[IDValue] => 12345678.0
)
)
)
[2] => SimpleXMLElement Object
(
[#attributes] => Array
(
[CounterUnit] => Impressions
[DeviceCondition] => OK
[DeviceID] => 192.168.1.102
[DeviceStatus] => Running
[ProductionCounter] => 23456789
[StatusDetails] => Indigo: Printing
)
[GeneralID] => SimpleXMLElement Object
(
[#attributes] => Array
(
[IDUsage] => hpCustomerImpressionCounter
[IDValue] => 23456789.0
)
)
)
)
)
So far so good. But I need to get the data from the DeviceInfo array, so I modify the code:
<pre>
<?php print_r($jdf_response->Response->DeviceList->DeviceInfo); ?>
</pre>
But instead of three SimpleXMLElement objects, I get only the first.
SimpleXMLElement Object
(
[#attributes] => Array
(
[DeviceCondition] => OK
[DeviceID] => HPSSPP-SM
[DeviceStatus] => Running
[StatusDetails] => Running
)
)
What am I doing wrong?
Update:
The reason I was using print_r() in the first place because because I was getting no output from the following code:
<?php
$addresses = array();
foreach ($jdf_response->Response->DeviceList->DeviceInfo as $device) {
$addresses[] = $device->{'#attributes'}'DeviceID'];
}
print_r($addresses);
Example #4 Accessing non-unique elements in SimpleXML
When multiple instances of an element exist as children of a single parent element,
normal iteration techniques apply.
Data is still there, You just have to use an iterator like:
foreach($jdf_response->Response->DeviceList->DeviceInfo as $device)
{
print_r($device);
}
Reference
I would like to get methods from REST XML file via PHP.
I have local REST file, which is in this format:
SimpleXMLElement Object
(
[doc] => SimpleXMLElement Object
(
)
[resources] => SimpleXMLElement Object
(
[#attributes] => Array
(
[base] => https://**url**
)
[resource] => Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[path] => xml/{accesskey}/project
)
[param] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => accesskey
[style] => template
[type] => xs:string
)
)
[method] => SimpleXMLElement Object
(
[#attributes] => Array
(
[id] => getAllProjects
[name] => GET
)
[response] => SimpleXMLElement Object
(
[representation] => SimpleXMLElement Object
(
[#attributes] => Array
(
[mediaType] => application/xml; charset=utf-8
)
)
)
)
... and so on
I have the following code, but it returns just the first method name:
$file="application.wadl";
$xml = simplexml_load_file($file);
foreach($xml->resources[0]->resource->method->attributes() as $a => $b) {
echo $b,"\n";
}
I would like to extract all of them, not just the first one. How to do that?
Rather than looping over the attributes of one element, you need to loop over all the elements with the same name. Due to the magic of SimpleXML, this is as simple as this:
foreach($xml->resources->resource->method as $method) {
echo $method['id'],"\n";
}
When followed immediately by another operator, as with ->resources, SimpleXML assumes you just want the first element with that name. But if you loop over, it will give you each of them, as a SimpleXML object.
EDIT : It looks like the nesting of your XML means you need some form of recursion (you need to look at $xml->resources->resource->resource->resource->method etc).
Something like this perhaps (untested example)?
function get_methods($base_url, $node)
{
$all_methods = array();
// Child resources: build up the path, and recursively fetch all methods
foreach ( $node->resource as $child_resource )
{
$child_url = $base_url . '/' . (string)$child_resource['path'];
$all_methods = array_merge(
$all_methods,
get_methods($child_url, $child_resource)
);
}
// Methods in this resource: add to array directly
foreach ( $node->method as $method )
{
$method_url = $base_url . '/' .(string)$method['id'];
$all_methods[$method_url] = (string)$method['id'];
}
return $all_methods;
}
print_r( get_methods('/', $xml->resources) );
Incidentally, print_r won't always give you the best view of a SimpleXML object, because they are actually wrappers around non-PHP code. Try this simplexml_dump() function instead.
Here is my code:
$string = <<<XML
<?xml version='1.0'?>
<test>
<testing>
<lol>hello</lol>
<lol>there</lol>
</testing>
</test>
XML;
$xml = simplexml_load_string($string);
echo "All of the XML:\n";
print_r $xml;
echo "\n\nJust the 'lol' array:";
print_r $xml->testing->lol;
Output:
All of the XML:
SimpleXMLElement Object
(
[testing] => SimpleXMLElement Object
(
[lol] => Array
(
[0] => hello
[1] => there
)
)
)
Just the 'lol' array:
SimpleXMLElement Object
(
[0] => hello
)
Why does it output only the [0] instead of the whole array? I dont get it.
What #Yottatron suggested is true, but not at all the cases as this example shows :
if your XML would be like this:
<?xml version='1.0'?>
<testing>
<lol>
<lolelem>Lol1</lolelem>
<lolelem>Lol2</lolelem>
<notlol>NotLol1</lolelem>
<notlol>NotLol1</lolelem>
</lol>
</testing>
Simplexml's output would be:
SimpleXMLElement Object
(
[lol] => SimpleXMLElement Object
(
[lolelem] => Array
(
[0] => Lol1
[1] => Lol2
)
[notlol] => Array
(
[0] => NotLol1
[1] => NotLol1
)
)
)
and by writing
$xml->lol->lolelem
you'd expect your result to be
Array
(
[0] => Lol1
[1] => Lol2
)
but instead of it, you would get :
SimpleXMLElement Object
(
[0] => Lol1
)
and by
$xml->lol->children()
you would get:
SimpleXMLElement Object
(
[lolelem] => Array
(
[0] => Lol1
[1] => Lol2
)
[notlol] => Array
(
[0] => NotLol1
[1] => NotLol1
)
)
What you need to do if you want only the lolelem's:
$xml->xpath("//lol/lolelem")
That gives this result (not as expected shape but contains the right elements)
Array
(
[0] => SimpleXMLElement Object
(
[0] => Lol1
)
[1] => SimpleXMLElement Object
(
[0] => Lol2
)
)
It's because you have two lol elements. In order to access the second you need to do this:
$xml->testing->lol[1];
this will give you "there"
$xml->testing->lol[0];
Will give you "hello"
The children() method of the SimpleXMLElement will give you an object containing all the children of an element for example:
$xml->testing->children();
will give you an object containing all the children of the "testing" SimpleXMLElement.
If you need to iterate, you can use the following code:
foreach($xml->testing->children() as $ele)
{
var_dump($ele);
}
There is more information about SimpleXMLElement here:
http://www.php.net/manual/en/class.simplexmlelement.php
what you might want to do is using the Json encode/decode
$jsonArray = Json_decode(Json_encode($xml), true);
With the true argument you can call instead of using -> use [name]
so an example would be:
$xml = file("someXmlFile.xml");
$jsonArray = Json_decode(Json_encode($xml), true);
foreach(jsonArray['title'] as $title){
Do something with $titles
}
if you have more than 1 element it will typical put in an #attributes if the elements has attributes. This can be countered by using: $title = $title['#attributes']
Hope it could help.
Ah yes, I remember simple XML nearly doing my head in with this parsing arrays issue
Try the below code. It will give you an array of LOL elements, or, if you've just got a single LOL element, it will return that in an array as well.
The main advantage of that is you can do something like foreach ($lol as $element) and it will still work on a single (or on 0) LOL element.
<?php
$string = <<<XML
<?xml version='1.0'?>
<test>
<testing>
<lol>hello</lol>
<lol>there</lol>
</testing>
</test>
XML;
$xml = simplexml_load_string($string);
echo "<pre>";
echo "All of the XML:\n";
print_r($xml);
echo "\n\nJust the 'lol' array:\n";
$test_lols = $xml->testing->children();
$childcount = count($test_lols);
if ($childcount < 2) {
$lol = array($test_lols->lol);
}
else {
$lol = (array) $test_lols;
$lol = $lol['lol'];
}
print_r($lol);
?>
Ran into this issue...
Xpath can be a little slow, so you can achieve the same effect with a simple for loop.
for ($i = 0; $i < count($xml->testing->lol); $i++) {
print_r($xml->testing->lol[$i]);
}