Why is my SimpleXMLElement data truncated? - php

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

Related

SimpleXML Skipping Attributes

Test XML:
<?xml version="1.0" encoding="UTF-8"?>
<Transfer>
<ABR recordLastUpdatedDate="20180329" replaced="N">
<ABN status="ACT" ABNStatusFromDate="20000214">80007321682</ABN>
<EntityType>
<EntityTypeInd>PUB</EntityTypeInd>
<EntityTypeText>Australian Public Company</EntityTypeText>
</EntityType>
<MainEntity>
<NonIndividualName type="MN">
<NonIndividualNameText>BLACK CABS COMBINED PTY LTD</NonIndividualNameText>
</NonIndividualName>
<BusinessAddress>
<AddressDetails>
<State>VIC</State>
<Postcode>3166</Postcode>
</AddressDetails>
</BusinessAddress>
</MainEntity>
</ABR>
</Transfer>
PHP Script:
$f='test.xml';
$reader=new XMLReader();
$reader->open($f);
while($reader->read()){
if($reader->nodeType==XMLReader::ELEMENT && $reader->name=='ABR'){
$doc=new DOMDocument('1.0','UTF-8');
$xml=simplexml_import_dom($doc->importNode($reader->expand(),true));
print_r($xml);
}
}
$reader->close();
PHP Output:
SimpleXMLElement Object
(
[#attributes] => Array
(
[recordLastUpdatedDate] => 20180329
[replaced] => N
)
[ABN] => 80007321682
[EntityType] => SimpleXMLElement Object
(
[EntityTypeInd] => PUB
[EntityTypeText] => Australian Public Company
)
[MainEntity] => SimpleXMLElement Object
(
[NonIndividualName] => SimpleXMLElement Object
(
[#attributes] => Array
(
[type] => MN
)
[NonIndividualNameText] => BLACK CABS COMBINED PTY LTD
)
[BusinessAddress] => SimpleXMLElement Object
(
[AddressDetails] => SimpleXMLElement Object
(
[State] => VIC
[Postcode] => 3166
)
)
)
)
The Problem:
The attributes for the ABN element (status and ABNStatusFromDate) are not in the output, even though other attributes are.
Please help me understand why those attributes in particular are missing.
PS - Dummy text so SO doesn't give me warnings about my post being mostly code
Answer: print_r is not meant to be used to display a SimpleXML object.
I can access the attribute directly via $xml->ABN['status'].

Pull values from SimpleXMLElement Object

I'm using the namecheap API to do some stuff, it's the first time I've used a API and I'm running into a bit of a problem.
This is what I have so far:
$ApiKey = "**********************";
$ApiUser = "*****";
$UserName = "*********";
$ClientIP = "********";
$NamecheapURI = "https://api.namecheap.com/xml.response";
$executionURL = $NamecheapURI."?ApiUser=".$ApiUser."&ApiKey=".$ApiKey."&UserName=".$UserName."&Command=namecheap.domains.check&ClientIp=".$ClientIP."&DomainList=".$domain;
$xml = simplexml_load_file($executionURL);
print_r($xml);
When print $xml I am returned simple XML objects:
SimpleXMLElement Object
(
[#attributes] => Array
(
[Status] => OK
)
[Errors] => SimpleXMLElement Object
(
)
[Warnings] => SimpleXMLElement Object
(
)
[RequestedCommand] => namecheap.domains.check
[CommandResponse] => SimpleXMLElement Object
(
[#attributes] => Array
(
[Type] => namecheap.domains.check
)
[DomainCheckResult] => SimpleXMLElement Object
(
[#attributes] => Array
(
[Domain] => facebook.com
[Available] => false
[ErrorNo] => 0
[Description] =>
[IsPremiumName] => false
[PremiumRegistrationPrice] => 0
[PremiumRenewalPrice] => 0
[PremiumRestorePrice] => 0
[PremiumTransferPrice] => 0
[IcannFee] => 0
[EapFee] => 0
)
)
)
[Server] => PHX01APIEXT03
[GMTTimeDifference] => --5:00
[ExecutionTime] => 0.008
)
My question is beyond this, how do I move forward and pull data from this?
I've tried treating this as an array but I am getting nowhere, when using is_array() to test if it was an array it says it's not which I don't understand...
I apologise if this is a noob question, I am a bit new to this. In short, what do I need to do to pull data from this?
Thanks in advance!
Learning to use SimpleXML is much better than trying to convert it to arrays/json/anything else and simple (hence the name). A quick example...
$response = '<?xml version="1.0" encoding="UTF-8"?>
<CommandResponse Type="namecheap.domains.check">
<DomainCheckResult Domain="facebook.com">
<Element>1234</Element>
<Element>12345</Element>
</DomainCheckResult>
</CommandResponse>';
$xml = simplexml_load_string($response);
echo "DOmain=".$xml->DomainCheckResult['Domain'].PHP_EOL;
foreach ( $xml->DomainCheckResult->Element as $value) {
echo "Value=".(string)$value.PHP_EOL;
}
outputs...
DOmain=facebook.com
Value=1234
Value=12345
You have to adapt this to your own XML, but the idea is that if you want to access an element of an item you use object notation -> and if you need to get an attribute, use array notation [].
So in the above code, the first echo ($xml->DomainCheckResult['Domain']) gets the <DomainCheckResult> element and outputs the Domain attribute.
Then the foreach loop says fetch each <Element> within <DomainCheckResult> and output the value.

Export Array of SimpleXMLElement objects to MySQL Database

I have a SOAP Response from a Web Service and have extracted the XML data as a SimpleXMLElement. I have then iterated through this object to extract the various fields I need and save them into an array, which becomes an array of SimpleXMLElement objects.
I am now trying to export this data into a MySQL Database which, according to my research, means turning the array into a String and then using mysql_query("INSERT INTO (whatever) VALUES (whatever)");. I have tried implode and serialize but neither work and I get the error:
Fatal error: Uncaught exception 'Exception' with message 'Serialization of 'SimpleXMLElement' is not allowed'
This is what the array I have created from the SimpleXMLELement looks like:
Array
(
[0] => Array
(
[uid] => SimpleXMLElement Object
(
[0] => WOS:000238186400009
)
[journal] => SimpleXMLElement Object
(
[#attributes] => Array
(
[type] => source
)
)
[publication] => SimpleXMLElement Object
(
[#attributes] => Array
(
[type] => item
)
[0] => Abundance of hedgehogs (Erinaceus europaeus) in relation to the density and distribution of badgers (Meles meles)
)
[year] => 2006
[author1] => SimpleXMLElement Object
(
[0] => Young, RP
)
[address] => SimpleXMLElement Object
(
[0] => Cent Sci Lab, Sand Hutton, Yorks, England
)
[author2] => SimpleXMLElement Object
(
[0] => Davison, J
)
[author3] => SimpleXMLElement Object
(
[0] => Trewby, ID
)
[citations] => SimpleXMLElement Object
(
[#attributes] => Array
(
[local_count] => 15
[coll_id] => WOS
)
)
) ... etc ...
)
Can anyone help me with the method to get this data into my database, please? Do I need to change it into (yet) another format?
You have to iterate through your array to create a new array fulfilled with strings instead of SimpleXMLElement, such as :
<?php
// your array (already built)
$arraySimpleXml = array(
"example1" => new SimpleXMLElement("<test>value</test>"),
"example2" => new SimpleXMLElement("<test>value2</test>")
);
// output array, to store in database
$result = array();
foreach($arraySimpleXml as $key => $simpleXml) {
$result[$key] = $simpleXml->asXML();
}
// gets your result as a string => you can now insert it into mysql
$dbInsertion = serialize($result);
?>
So I worked out how to change the data into a standard array rather than an array of SimpleXMLElements so that I can successfully insert it into a MySQL database.
When iterating the SimpleXMLElement object to extract the data I needed I cast the type as String so that now my array has the format (as opposed to above):
Array
(
[0] => Array
(
[uid] => WOS:000238186400009
[journal] => JOURNAL OF ZOOLOGY
[publication] => Abundance of hedgehogs (Erinaceus europaeus) in relation to the density and distribution of badgers (Meles meles)
[year] => 2006
[author1] => Young, RP
[address] => Cent Sci Lab, Sand Hutton, Yorks, England
[author2] => Davison, J
[author3] => Trewby, ID
[citations] => 15
)
)
Thought I'd post this in case anyone has a similar problem in future. To do this, when iterating the data instead of:
$uid = $record->UID;
I did:
$uid = (string)$record->UID;
For each of the data fields I required. This ensures the data is stored as a String and so removes the SimpleXMLElement format.

Get Methods from REST call

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.

Loading XML using SimpleXML doesn't return attributes on some elements

I have loaded an XML file using
simplexml_load_file($filePath,'SimpleXMLElement', LIBXML_NOCDATA);
And for most of the XML provided it works fine. However, for some of the elements in the XML the attributes are not converted into an '#attributes' array, and are instead missing form the output. Here's a sample:
<UI_DEFINITION>
<EDIT_PERMISSION>testPermission</EDIT_PERMISSION>
<DEFAULT_VALUES>
<display>hidden</display>
<css_class>generic_css_class</css_class>
<title>{tag}</title>
<type>string</type>
<wrapper_format>{value}</wrapper_format>
<full_path>false</full_path>
<mandatory>false</mandatory>
<edit_permission>testPermission</edit_permission>
<max_length>0</max_length>
</DEFAULT_VALUES>
<LOOKUPS>
<DB_LOOKUP name="test3">
<VIEW>???</VIEW>
<ID_FIELD>???</ID_FIELD>
<DESCR_FIELD>???</DESCR_FIELD>
<ORDER>??? asc</ORDER>
</DB_LOOKUP>
<DB_LOOKUP name="test1">
<VIEW>???</VIEW>
<ID_FIELD>???</ID_FIELD>
<DESCR_FIELD>???</DESCR_FIELD>
<ORDER>??? asc</ORDER>
</DB_LOOKUP>
</LOOKUPS>
<AREA internal_name="main_details" title="" display="show">
<FIELD lookup="test1" title="Title">Title</FIELD>
<FIELD title="Name">Given_Name</FIELD>
<FIELD title="Mid. Name(s)">Middle_Names</FIELD>
<FIELD title="Family Name">Family_Name</FIELD>
<FIELD title="Gender">Gender</FIELD>
<FIELD title="Born" type="date">Date_of_Birth</FIELD>
<FIELD max_length="20" title="ID">Unique_Identifier</FIELD>
</AREA>
This gives the following output from print_r (I've added a line break at the bit that's the problem):
SimpleXMLElement Object ( [UI_DEFINITION] => SimpleXMLElement Object ( [EDIT_PERMISSION] => testPermission [DEFAULT_VALUES] => SimpleXMLElement Object ( [display] => hidden [css_class] => generic_css_class [title] => {tag} [type] => string [wrapper_format] => {value} [full_path] => false [mandatory] => false [edit_permission] => testPermission [max_length] => 0 ) [LOOKUPS] => SimpleXMLElement Object ( [DB_LOOKUP] => Array ( [0] => SimpleXMLElement Object ( [#attributes] => Array ( [name] => test3 ) [VIEW] => ??? [ID_FIELD] => ??? [DESCR_FIELD] => ??? [ORDER] => ??? asc ) [1] => SimpleXMLElement Object ( [#attributes] => Array ( [name] => test1 ) [VIEW] => ??? [ID_FIELD] => ??? [DESCR_FIELD] => ??? [ORDER] => ??? asc ) ) )
[AREA] => SimpleXMLElement Object ( [#attributes] => Array ( [internal_name] => main_details [title] => [display] => show ) [FIELD] => Array ( [0] => Title [1] => Given_Name [2] => Middle_Names [3] => Family_Name [4] => Gender [5] => Date_of_Birth [6] => Unique_Identifier ) ) ) )
As you can see, the attributes array is correctly added to most of the elements, but not to the FIELD elements. I've tried renaming them and it didn't seem to make a difference.
EDIT:
I should also add that I've tried surrounding the FIELD tags with a FIELDS tag, also to no avail.
EDIT:
I've simplified the XML hugely, and it still doesn't return anny attributes:
<UI_DEFINITION>
<FIELD lookup="test1" title="Title">Title</FIELD>
</UI_DEFINITION>
produces:
SimpleXMLElement Object ( [UI_DEFINITION] => SimpleXMLElement Object ( [FIELD] => Title ) )
The attributes are accessible, for example:
$obj = simplexml_load_string($xml);
foreach($obj->AREA->FIELD as $field)
{
echo $field->attributes()->title . '<br />';
}
print_r() does not always show the full structure with SimpleXML, but the attributes are there for use.
Sorry it's taken so long to come back and answer this question!
As MrCode suggested, the attributes were accessible. The problem I was in the serialisation of the SimpleXML object into another format. Using printr or json_convert on the while object resulted in the attributes not being available in the cases reported.
I didn't go far enough into this to find a code-based workaround for printing or converting these objects including the problematic cases, I simply worked around it as part of the XML data:
<UI_DEFINITION>
<FIELD lookup="test1" title="Title"><VALUEPATH>Title</VALUEPATH></FIELD>
</UI_DEFINITION>
Addint this extra level into the hierarchy resulted in the attributes being preserved at the top level, and the text value being available correctly at the sub-level.

Categories