I tried to parse this using lots of way I found but could't parse this.
<?xml version="1.0" encoding="UTF-16"?>
<iBridge xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<User ID="162715" TOKEN="ABE02FD9-89FA-47F4-B687-E75D7AB63F4B" USER_COOKIE="ASP.NET_SessionId=g2b3pl453rmbatjpsitjz255|Login=|Login=8D34CBFE9FD0108D506BD78D42E76CB0B424FB1335DA48E8AA52D11466BAC8EE55FABB019D1C81E6A7E91A62E47C3DE049CB9C3BFBC680F50208282F8BA2451C2202585E49D62F1A" FULL_NAME="Sachin Kumar" FIRST_NAME="Sachin" LAST_NAME="Kumar" COMPANY="Unikove" LAST_FIRST="KUMAR, SACHIN" ADDRESS_1="Address One" ADDRESS_2="" CITY="My City" STATE_PROVINCE="AE" ZIP="90001" COUNTRY="" PREFERRED_MAIL="1" PREFERRED_BILL="1" CO_ID="" MEMBER_TYPE="WEB" MEMBER_TYPE_DESCRIPTION="Web Sign-Up" EMAIL="sachin.kumar#unikove.com" ASSIGNMENT="" />
</iBridge>
if you save your xml file in the root of your php script and call it my_xml.xml , the php script would be :
<?php
$iBridge = simplexml_load_file('my_xml.xml');
foreach ($iBridge as $userinfo){
$id = $userinfo['ID'];
$token = $userinfo['TOKEN'];
$user_cookie = $userinfo['USER_COOKIE'];
echo "ID: '".$id."<br/>TOKEN: ".$token."<br/>USER COOKIE: ".$user_cookie."<br/>";
}
?>
You can add the rest of properties yourself just like the pattern above, FIRST_NAME, LAST_NAME,etc..
Related
This is XML file.
<?xml version="1.0" encoding="utf-8"?>
<UW xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/XMLSchema.xsd">
<UWdata>
<List>
<IdProduct>1</IdProduct>
<ProductName>product</ProductName>
<ProductNameDE>product</ProductNameDE>
<ProductNameEN>product</ProductNameEN>
<Uf>1</Uf>
<PSIg>1</PSIg>
<Ug>1</Ug>
</List>
</UWdata>
</UW>
$lines_array=file($url);
$lines_string=implode('',$lines_array);
$xml=simplexml_load_string($lines_string) or die("Error: Cannot create object");
I try with this
echo $xml->UWdata[1]->ProductName;
But it doesn't return anything.I want to return Product name.
Sample code, Use simplexml_load_string
<?php
$a = '<?xml version="1.0" encoding="utf-8"?>
<UW xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/XMLSchema.xsd">
<UWdata>
<List>
<IdProduct>1</IdProduct>
<ProductName>product</ProductName>
<ProductNameDE>product</ProductNameDE>
<ProductNameEN>product</ProductNameEN>
<Uf>1</Uf>
<PSIg>1</PSIg>
<Ug>1</Ug>
</List>
</UWdata>
</UW>';
$xml=simplexml_load_string($a) or die("Error: Cannot create object");
echo ($xml->UWdata->List->ProductName);
?>
When you load the xml file using the php simplexml_load_file function to a variable. The veritable becomes an object.
<?php
$xml=simplexml_load_file("/path/to/the/file.xml");
?>
So, in your case, the $xml variable becomes a multi-level object where every elements of xml file are key of the object. Like: UWdata.
So, as $xml is a multi-level object, to access the element under UWdata, under List under ProductName, you have to code like bellow.
echo $xml->UWdata->List->ProductName."<br>";
Here,
UWdata is the key of $xml object.
List is the key of UWdata.
ProductName is the key of List.
Finally, you will get the value of key element ProductName = product
I modified your script and put the xml in an external file called testxml.xml, as it should be. Always separate the function and the data it's supposed to handle. I used your xml like this:
<?xml version="1.0" encoding="utf-8"?>
<UW xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://tempuri.org/XMLSchema.xsd">
<UWdata>
<List>
<IdProduct>1</IdProduct>
<ProductName>productTEST</ProductName>
<ProductNameDE>product</ProductNameDE>
<ProductNameEN>product</ProductNameEN>
<Uf>1</Uf>
<PSIg>1</PSIg>
<Ug>1</Ug>
</List>
</UWdata>
</UW>
And with the following script it returns productTEST only.
$xmlstr = file_get_contents('./testxml.xml');
$xml = simplexml_load_string($xmlstr);
$json = json_encode($xml);
$array = json_decode($json,TRUE);
echo $array['UWdata']['List']['ProductName'];
Hope this helps.
//edit:
While I do not know your project, you might want to take a foreach-approach if it is possible for your xml to contain more than one List element
I've been trying to figure this out for over an hour now, and I give up. I'm getting the following response from a webservice.
I want to be able to get access to the children of Fault (I have this working for another webservice call, but for some reason, which I believe MIGHT be namespace related, this one doesn't work).
<soap:envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<soap:Fault>
<faultcode>soap:Server</faultcode>
<faultstring>Invalid Scope</faultstring>
<detail>
<faultresponse xmlns="xsd url" xmlns:ns2="xsd url">
<transactionid>TEST</transactionid>
<errorcode>ERR-4223</errorcode>
</faultresponse>
</detail>
</soap:Fault>
</soap:Body>
</soap:envelope>
My PHP code is pretty simple. Firstly, I create the SimpleXmlElement object from the above xml.
I get a list of namespaces in the xml (which it picks up two: "soap", and ""). I then get the children of the XML in the soap: namespace.
$xml = simplexml_load_string($response_xml);
$namespace = $xml->getNamespaces(true);
$soap = $xml->children($namespace['soap']);
Given the above code. I would expect to be able to do something like this:
$fault_fields = $soap->Body->children()->Fault->children();
foreach ($fault_fields as $field):
echo (string) $field->getName() . ' - ' . $field[0] . '<br />';
endforeach;
However, if I run $soap->asXML(); I can see the following:
<soap:Body>
<soap:Fault>
<faultcode>soap:Server</faultcode>
<faultstring>Invalid Scope</faultstring>
<detail>
<faultresponse xmlns="xsd url" xmlns:ns2="xsd url">
<transactionid>TEST</transactionid>
<errorcode>ERR-4223</errorcode>
</faultresponse>
</detail>
</soap:Fault>
</soap:Body>
But if I try to go to access Body or Fault:
$soap = &$this->parse_soap_body($response_xml);
$body = $soap->Body->children()->Fault->children();
echo $body->asXML();
I get a Node no longer exists error with the stack returning this XML.
<soap:Envelope xmlns:soap="`http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<soap:Fault>
<faultcode>soap:Server</faultcode>
<faultstring>Invalid Scope</faultstring>
.... more xml
This is seriously doing my head in.
Any help would be greatly appreciated.
SO,
I'm receiving the following SOAP response as a string:
<?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>
<GetListItemsResponse xmlns="http://schemas.microsoft.com/sharepoint/soap/">
<GetListItemsResult>
<listitems xmlns:s="uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882" xmlns:dt="uuid:C2F41010-65B3-11d1-A29F-00AA00C14882" xmlns:rs="urn:schemas-microsoft-com:rowset" xmlns:z="#RowsetSchema">
<rs:data ItemCount="3">
<z:row ows_DocIcon="jpg" ows_LinkFilename="18380014229851.jpg" ows_Modified="2016-10-08 17:27:40" ows_Editor="179440;#asdf" ows_Last_x0020_Modified="2;#2016-10-08 17:29:29" ows_ID="2" ows_Created_x0020_Date="2;#2016-10-08 17:27:40" ows_FileLeafRef="2;#18380014229851.jpg" />
<z:row ows_DocIcon="jpg" ows_LinkFilename="18380014229851_2.jpg" ows_Modified="2016-10-08 17:27:40" ows_Editor="179440;#asfd" ows_Last_x0020_Modified="3;#2016-10-08 17:29:29" ows_ID="3" ows_Created_x0020_Date="3;#2016-10-08 17:27:41" ows_FileLeafRef="3;#18380014229851_2.jpg" />
<z:row ows_DocIcon="jpg" ows_LinkFilename="18380014229851_3.jpg" ows_Modified="2016-10-08 17:27:40" ows_Editor="179440;#asdf" ows_Last_x0020_Modified="4;#2016-10-08 17:30:03" ows_ID="4" ows_Created_x0020_Date="4;#2016-10-08 17:27:41" ows_FileLeafRef="4;#18380014229851_3.jpg" />
</rs:data>
</listitems>
</GetListItemsResult>
</GetListItemsResponse>
</soap:Body>
</soap:Envelope>
I am attempting to get each of the "z:row" entries, but am struggling due to the namespaces (after some googling that's what i'm understanding them to be called).
Here is the code I am using:
$xml = simplexml_load_string($sp->soapClient->__last_response);
foreach($xml->GetListItemsResult as $item)
{
$ns_li = $item->children('uuid:BDC6E3F0-6DA3-11d1-A2A3-00AA00C14882');
$ns_rs = $ns_li->children('urn:schemas-microsoft-com:rowset');
$ns_z = $nr_rs->children('#RowsetSchema');
echo $ns_z->row;
}
Right now I am getting no output from echo statement. What am I doing wrong?
You can use XPath expression to get specific part of XML document using various criteria. For example, to get z:row elements anywhere in the XML document you can simply do //z:row, after registering the prefix z :
$xml->registerXPathNamespace("z", "#RowsetSchema");
foreach($xml->xpath('//z:row') as $item)
{
echo $item["ows_LinkFilename"] ."\n";
}
eval.in demo
output :
18380014229851.jpg
18380014229851_2.jpg
18380014229851_3.jpg
I must be missing something simple here and it's driving me crazy.
I'm making a call to a web services api and getting back some xml:
<?xml version="1.0" encoding="utf-16"?>
<MTSMember xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<LastName>Smith</LastName>
<FirstName>john</FirstName>
<MemberNo>xxxxxxxx</MemberNo>
<Club>None</Club>
<JoinDate>2013-05-14</JoinDate>
<Email>email#email.com</Email>
</MTSMember>
I then need to process this xml to get the email address. but I'm just getting an empty result using the code below:
$xml_result = simplexml_load_string($xml_string_above);
echo $xml_result->MTSMember[0]->Email;
Can someone point me in the right direction. I've read through several other answers trying out various solutions, but can't seem to get it to work.
Edit: This was the last tutorial i tried out http://blog.teamtreehouse.com/how-to-parse-xml-with-php5
That should be:
echo $xml_result->Email;
Because simplexml_load_string() is loading MTSMember as main SimpleXMLElement.
Codepad Example
Try this
$xml=new SimpleXMLElement($str);
$result=$xml->xpath('//Email');
foreach ($result as $Email)
echo $Email . "<br>";
simply
$xml=new SimpleXMLElement($str);
echo $xml[0]->Email;
update:
The below just for your comment , the whole what i tried
$str='<?xml version="1.0" encoding="utf-8"?>
<MTSMember xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<LastName>Smith</LastName>
<FirstName>john</FirstName>
<MemberNo>xxxxxxxx</MemberNo>
<Club>None</Club>
<JoinDate>2013-05-14</JoinDate>
<Email>email#email.com</Email>
</MTSMember>';
$xml=new SimpleXMLElement($str);
echo $xml[0]->Email;
//OR
$xml=new SimpleXMLElement($str);
$result=$xml->xpath('//Email');
foreach ($result as $Email)
echo $Email . "<br>";
Be happy :)
This should work fine
$xml = new SimpleXMLElement($xml_string_above);
$dc = $xml->email;
echo $dc;
I am trying to get to a part in an xml response, without following the whole path. Now I know that xpath has search abilities, but somehow I dont understand it... :(
The XML i am parsing is this:
<?xml version="1.0" encoding="utf-8"?>
<soapEnvelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsa="http://schemas.xmlsoap.org/ws/2004/08/addressing" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd" xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd">
<envHeader xmlns:env="http://www.w3.org/2003/05/soap-envelope">
<wsaAction>http://www.rechtspraak.nl/namespaces/cir01/searchUndertakingResponse</wsaAction>
<wsaMessageID>urn:uuid:11f7d4cd-2280-4298-85eb-dadf5bd743f1</wsaMessageID>
<wsaRelatesTo>urn:uuid:59630fbd-b990-4020-9c1c-822c58186d96</wsaRelatesTo>
<wsaTo>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsaTo>
<wsseSecurity>
<wsuTimestamp wsu:Id="Timestamp-df25f141-fed2-47ed-967e-93cd04d1c8f2">
<wsuCreated>2011-04-02T06:52:52Z</wsuCreated>
<wsuExpires>2011-04-02T06:57:52Z</wsuExpires>
</wsuTimestamp>
</wsseSecurity>
</envHeader>
<soapBody>
<searchUndertakingResponse xmlns="http://www.rechtspraak.nl/namespaces/cir01"><searchUndertakingResult>
<publicatieLijst xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" extractiedatum="2011-04-02T08:52:51" xmlns="http://www.rechtspraak.nl/namespaces/inspubber01">
<publicatieKenmerk>sgr.10.787.F.1300.1.10</publicatieKenmerk>
<publicatieKenmerk>utr.10.585.F.1300.1.10</publicatieKenmerk>
</publicatieLijst>
</searchUndertakingResult>
</searchUndertakingResponse>
</soapBody>
</soapEnvelope>
And I am looking for these values: <publicatieKenmerk>sgr.10.787.F.1300.1.10</publicatieKenmerk> <publicatieKenmerk>utr.10.585.F.1300.1.10</publicatieKenmerk>
Now this works:
$lijst = $results->soapBody->searchUndertakingResponse->searchUndertakingResult->publicatieLijst->publicatieKenmerk;
foreach ($lijst AS $kenmerk) {
echo $kenmerk."<BR>";
}
But I dont want to use this, as I need to be flexible for other results. and cannot rely on
searchUndertakingResponse->searchUndertakingResult
So I was hoping to use xpath to get there, but this doesnt work:
$lijst = $results->xpath('//publicatieKenmerk');
foreach($lijst as $kenmerk) {
echo $kenmerk."<br />";
}
But I thought relative would work as well... any ideas?
You'll notice that your <publicatieLijst> node has a default namespace set: xmlns="http://www.rechtspraak.nl/namespaces/inspubber01" which means that <publicatieKenmerk> exists that namespace. That's why //publicatieKenmerk won't find it, you have to search in the right namespace.
For that, you can register the namespace with your own prefix and use that prefix in the following XPath query, like this:
$soapEnvelope = simplexml_load_string($xml);
$soapEnvelope->registerXPathNamespace(
'inspubber01',
'http://www.rechtspraak.nl/namespaces/inspubber01'
);
foreach ($soapEnvelope->xpath('//inspubber01:publicatieKenmerk') as $publicatieKenmerk)
{
echo $publicatieKenmerk, "\n";
}
can you try to change on they way it loops?
while(list( , $node) = each($lijst)) {
echo 'kenmerk: ',$node,"\n";
}