Hey guys, I have looked all around for help but found nothing sadly. I am trying to use PHP to grab the contents of an XML file and pull the data from an attribute. I have checked other tutorials and tried but none worked.
Here is the XML file:
<eveapi version="2">
<currentTime>2011-04-03 03:55:59</currentTime>
<result>
<rowset name="notifications" key="notificationID" columns="notificationID,typeID,senderID,sentDate,read">
<row notificationID="339040500" typeID="75" senderID="1000137" sentDate="2011-04-03 03:53:00" read="0" />
</rowset>
</result>
<cachedUntil>2011-04-03 04:25:59</cachedUntil>
</eveapi>
I am trying to pull the data from 'typeID' & 'sentDate'. Also, this XML file may containt multiple tags.
All help is welcomed, thanks!
You can use the following code :-
$xmlstr = '<eveapi version="2">
<currentTime>2011-04-03 03:55:59</currentTime>
<result>
<rowset name="notifications" key="notificationID" columns="notificationID,typeID,senderID,sentDate,read">
<row notificationID="339040500" typeID="75" senderID="1000137" sentDate="2011-04-03 03:53:00" read="0" />
</rowset>
</result>
<cachedUntil>2011-04-03 04:25:59</cachedUntil>
</eveapi>
';
$xml = simplexml_load_string($xmlstr);
print_r ($xml->result->rowset->row['typeID']);
Note: If you have multiple row in rowset then in object, the row will be as collection of array. In that case you have to access the typeID like bellow -
print_r ($xml->result->rowset->row[0]['typeID']);
Putting flame retardant suit on...
$string = '<eveapi version="2">
<currentTime>2011-04-03 03:55:59</currentTime>
<result>
<rowset name="notifications" key="notificationID" columns="notificationID,typeID,senderID,sentDate,read">
<row notificationID="339040500" typeID="75" senderID="1000137" sentDate="2011-04-03 03:53:00" read="0" />
</rowset>
</result>
<cachedUntil>2011-04-03 04:25:59</cachedUntil>
</eveapi>';
preg_match_all('#([\S]+)="(.*?)"#is', $string, $matches);
unset($matches[0]);
$xml = array_combine($matches[1], $matches[2]);
print_r($xml);
=
Array
(
[version] => 2
[name] => notifications
[key] => notificationID
[columns] => notificationID,typeID,senderID,sentDate,read
[notificationID] => 339040500
[typeID] => 75
[senderID] => 1000137
[sentDate] => 2011-04-03 03:53:00
[read] => 0
)
Related
I'm very new to php so go easy on me.
I'm trying to search through a KML file for latitude and longitude values, then replace them with user inputted lat/long values. The problem I'm having is actually searching through the KML file to find the specific lat/long values to be replaced.
KML:
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
<Folder>
<name>Folder-Name</name>
<open>1</open>
<gx:Tour>
<name class="name">Tour-Name</name>
<gx:Playlist>
<gx:FlyTo>
<LookAt>
<gx:horizFov>100</gx:horizFov>
<longitude class="lookat-long">33.33333</longitude>
<latitude class="lookat-lat">-111.11111</latitude>
<altitude>0</altitude>
<heading>0</heading>
<tilt>60</tilt>
<range>100</range>
<altitudeMode>relativeToGround</altitudeMode>
</LookAt>
</gx:FlyTo>
</gx:Playlist>
</gx:Tour>
</Folder>
</kml>
I need to replace "33.33333" and "-111.11111" with user inputted values. I have tried using SimpleXML, but it does not recognize the gx: part of the tag, since that is KML specific, and not part of XML. So when I try this code:
<?php
$xml = simplexml_load_file('my_kml_file');
print_r($xml)
?>
I get this output:
SimpleXMLElement Object ( [Folder] => SimpleXMLElement Object ( [name] => Temporary Places [open] => 0 ) )
It just stops at <open> because it doesn't recognize the rest. I have spent hours upon hours trying to figure out how to best do this and I just can't. Please help.
You can use the xpath method to get the namespaced node like so:
$xml = simplexml_load_file('my_kml_file');
$xml->xpath('//gx:Tour/gx:Playlist/gx:FlyTo')[0]->LookAt->longitude = 'newValue';
$xml->xpath('//gx:Tour/gx:Playlist/gx:FlyTo')[0]->LookAt->latitude = 'newValue';
print_r($xml->asXml());
Output:
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2" xmlns:gx="http://www.google.com/kml/ext/2.2" xmlns:kml="http://www.opengis.net/kml/2.2" xmlns:atom="http://www.w3.org/2005/Atom">
<Folder>
<name>Folder-Name</name>
<open>1</open>
<gx:Tour>
<name class="name">Tour-Name</name>
<gx:Playlist>
<gx:FlyTo>
<LookAt>
<gx:horizFov>100</gx:horizFov>
<longitude class="lookat-long">newValue</longitude>
<latitude class="lookat-lat">newValue</latitude>
<altitude>0</altitude>
<heading>0</heading>
<tilt>60</tilt>
<range>100</range>
<altitudeMode>relativeToGround</altitudeMode>
</LookAt>
</gx:FlyTo>
</gx:Playlist>
</gx:Tour>
</Folder>
</kml>
I've tried to read other tutorials & SO responses on this but I just can't seem to make it work :/
I'm able to make the SOAP request and get a response but I can't seem to parse the response.
$result = $client->GetAllAttributes($params);
And the resulting response xml is:
<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetAllAttributesResponse xmlns="http://connect2.askadmissions.net/webservices/">
<GetAllAttributesResult>
<result>
<code>1</code>
<ErrorMessage />
<returndata>
<attributes>
<attribute>
<type>attribute</type>
<level />
<name>text1321</name>
<mappingname><![CDATA[Extra-Curricular Interest]]></mappingname>
<datatype>Varchar2</datatype>
<size>35</size>
<validationexp />
</attribute>
<attribute> (same as above, several of these are returned</attribute>
</attributes>
</returndata>
</result>
</GetAllAttributesResult>
</GetAllAttributesResponse>
</soap:Body>
</soap:Envelope>
</xml>
I've tried
$xml = simplexml_load_string($client->__getLastResponse());
print_r($xml);
But it just prints "SimpleXMLElement Object ( ) "
I've tried
$responseXML = $client->__getLastResponse();
$xml = simplexml_load_string($responseXML);
$xml->registerXPathNamespace('soap', 'http://schemas.xmlsoap.org/soap/envelope/');
$xml->registerXPathNamespace('hob', 'http://connect2.askadmissions.net/webservices/');
$item = $xml->xpath('//hob:GetAllAttributesResult');
print_r($item);
and I get an array
Array
(
[0] => SimpleXMLElement Object
(
[0] => <result><code>1</code><ErrorMessage /><returndata><attributes><attribute> <type>attribute</type><level />
etc. (array is very long)
My problem comes when I try to step further into the tree. If I do
$item = $xml->xpath('//hob:GetAllAttributesResult/hob:result');
or
$item = $xml->xpath('//hob:GetAllAttributesResult/hob:code');
I end up with an empty array.
How do I step further into the tree?
Thank you very much for any help.
To access the element's value, you need to access the first element of the array. And you may need to cast it to string to get the string value
e.g. in your example you can do:
$item = $xml->xpath('//hob:GetAllAttributesResult/hob:result/hob:code');
print_r((string)$item[0]);
I have a KML file for google maps that I want to edit. Here's a stripped-down version:
<?xml version='1.0' encoding='UTF-8'?>
<kml xmlns='http://www.opengis.net/kml/2.2'>
<Document>
<Placemark>
<name>00</name>
<snippet></snippet>
<description><![CDATA[]]></description>
<styleUrl>#Style0-polygon-3-map</styleUrl>
<ExtendedData>
<Data name='Two-Digit Districts'>
<value>AK-00</value>
</Data>
<Data name='Standard Districts'>
<value>AK-AL</value>
</Data>
<Data name='At Large as District 1'>
<value>AK-1</value>
</Data>
<Data name='Full District Name'>
<value>Alaska At Large</value>
</Data>
<Data name=''>
<value>AK</value>
</Data>
</ExtendedData>
</PlaceMark>
</Document>
</kml>
In my php code, I'm digging into each placemark node using SimpleXML:
foreach ($kml->Document->Placemark as $placemark) {
$extendeddata = $placemark->ExtendedData;
}
I need to get the <Data> node where the name attribute matches "At Large as District 1". In the XML tools plugin for NPP, when I take just the <ExtendedData>...</ExtendedData> node, this query returns the node I want: //Data[#name='At Large as District 1']. However, when I try that same query in PHP:
$targetnode = $extendeddata->xpath("//Data[#name='At Large as District 1']")
I get an empty array. I don't understand why what should be a perfectly valid xpath query isn't returning any information. Am I missing something?
There is an ending tag mismatch.
Replace </PlaceMark> with </Placemark>
Code
You have to register with a namespace in order to do an xpath in this case.
$extendeddata = simplexml_load_string( $str );
$extendeddata->registerXPathNamespace( 'ns', 'http://www.opengis.net/kml/2.2' );
$targetnode = $extendeddata->xpath("//ns:Data[#name='At Large as District 1']");
Output
Array
(
[0] => SimpleXMLElement Object
(
[#attributes] => Array
(
[name] => At Large as District 1
)
[value] => AK-1
)
)
Hope this helps.
I have run into a minor problem. I am using Zoho CRM API and it returns me an XML in a format like this:
<response uri="/crm/private/xml/Contacts/getRecords">
<result>
<Contacts>
<row no="1">
<FL val="Contact Owner">
<![CDATA[ Kristo Vaher ]]>
</FL>
<FL val="Lead Source">
<![CDATA[ Partner ]]>
</FL>
</row>
</Contacts>
</result>
</response>
When I create an XML object through simplexml_load_string() then it will give me most of that XML in the new object, but it won't give me the 'inner' string of FL tags (the CDATA elements), the data that actually interests me.
My new SimpleXML object only has data like:
[1] => SimpleXMLElement Object
(
[#attributes] => Array
(
[val] => Contact Owner
)
)
My best guess is that this is because the XML should not really be built this way, I've read somewhere that your cannot have inner content in XML tag if it has attributes and vice versa (is this correct?).
What are my alternatives? Writing a parser myself is not really an option.
Thanks!
To get attributes:
foreach ($value->attributes() as $key => $val){
// get all attributes
}
To get data:
echo (string) $load->result->Contacts->row->FL[0];
I get this error:
Notice: Trying to get property of non-object in
Applies to:
echo $result->Data;
And this output:
Array ()
Background Informations
A function returns a string which contains an XML file.
I want to get some data from two tags and deal with them on their own.
String Data
$data="
<SyncML xmlns='SYNCML:SYNCML1.0'>
<SyncHdr>
</SyncHdr>
<SyncBody>
<betameta>
WANT 1
</betameta>
<Add>
<Data>
WANT 2
</Data>
</Add>
</SyncBody>
</SyncML>";
In the above data, I want values "WANT 1" and "WANT 2"
Code so far
$xml = simplexml_load_string($data);
$result = $xml->xpath("/SyncML/SyncBody");
print_r($result);
echo $result->Data;
$xml->registerXPathNamespace('default','SYNCML:SYNCML1.0');
$result = $xml->xpath("/default:SyncML/default:SyncBody");
Remove the trailing slash.
The only solution I can find is the following:
<?php
$data= <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<SyncML>
<SyncHdr>
</SyncHdr>
<SyncBody>
<betameta>
WANT 1
</betameta>
<Add>
<Data>
WANT 2
</Data>
</Add>
</SyncBody>
</SyncML>
XML;
$xml = simplexml_load_string($data);
$result = $xml->xpath("/SyncML/SyncBody");
print_r($result);
echo $result;
is there anyway you can loose the xmlns?
This will output:
Array
(
[0] => SimpleXMLElement Object
(
[betameta] =>
WANT 1
[Add] => SimpleXMLElement Object
(
[Data] =>
WANT 2
)
)
)