This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 6 years ago.
I have a problem with PHP. I cannot read xml with PHP, and with SimpleXml.
How can I do.
My Xml files is below.
I just want your CompanyList.
<?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" 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">
<soap:Header>
<wsa:Action>http://www.temp/Response</wsa:Action>
<wsa:MessageID>urn:uuid:eac190a5-c833-4dee-b4ef-fa81b0bad5c1</wsa:MessageID>
<wsa:RelatesTo>urn:uuid:834ace07-1e96-49d9-b958-3b2b87169917</wsa:RelatesTo>
<wsa:To>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:To>
<wsse:Security>
<wsu:Timestamp wsu:Id="Timestamp-55f3bafa-087a-4971-975d-a314957b4282">
<wsu:Created>2016-11-17T15:12:46Z</wsu:Created>
<wsu:Expires>2016-11-17T15:17:46Z</wsu:Expires>
</wsu:Timestamp>
</wsse:Security>
</soap:Header>
<soap:Body>
<ListResponse xmlns="http://fatura.edoksis.net">
<CompanyResult>
<Success>1</Sonuc>
<CompanyList>
<Company>
<Identifier>8380436088</Identifier>
<RegisterTime>2016-10-21T00:00:00Z</RegisterTime>
</Company>
<Company>
<Identifier>8380436088</Identifier>
<RegisterTime>2016-10-21T00:00:00Z</RegisterTime>
</Company>
</CompanyList>
</CompanyResult>
</CompanyResult>
</soap:Body>
</soap:Envelope>
$dom = new DOMDocument;
$dom->loadXML($response);
$books = $dom->getElementsByTagName('Identifier');
foreach ($books as $book) {
echo $book->nodeValue ."</br>";
}
Related
This question already has answers here:
SimpleXMLElement Access elements with namespace?
(4 answers)
Closed 1 year ago.
echo $xml->asXML();
Prints the following, and I am tring to access to elements here like: InvoiceStateResult
<?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"
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">
<soap:Header>
<wsa:Action>http://tempuri.org/SendEArchiveDataResponse</wsa:Action>
<wsa:MessageID>urn:uuid:72e8aaf0-b36d-422f-ab0b-486c17c50c83</wsa:MessageID>
<wsa:RelatesTo>urn:uuid:fc0a3e9d-40c1-4f3b-9517-8002825b7217</wsa:RelatesTo>
<wsa:To>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:To>
<wsse:Security>
<wsu:Timestamp wsu:Id="Timestamp-3a82271a-a910-4062-81aa-984468387047">
<wsu:Created>2021-05-28T12:12:23Z</wsu:Created>
<wsu:Expires>2021-05-28T12:27:23Z</wsu:Expires>
</wsu:Timestamp>
</wsse:Security>
</soap:Header>
<soap:Body>
<SendEArchiveDataResponse
xmlns="http://tempuri.org/">
<SendEArchiveDataResult>
<Invoices>
<InvoiceStateResult>
<ServiceResult>Error</ServiceResult>
<UUID>11111111-2222-3333-4444-555555555555</UUID>
<InvoiceId>T612014000000053</InvoiceId>
<StatusDescription>INVOICE EXISTS</StatusDescription>
<StatusCode>29</StatusCode>
<ErrorCode>0</ErrorCode>
<ReferenceNo>T612014000000053</ReferenceNo>
</InvoiceStateResult>
</Invoices>
<ServiceResult>Error</ServiceResult>
<ServiceResultDescription>This invoice processed before InvoiceId : TRL2021000000019 , UUID : DB3642EB-7A5F-40FD-8DF8-A922CA113837 SenderTaxID : 3324502175 . </ServiceResultDescription>
<Source>IntegrationWebService</Source>
<ErrorCode>30</ErrorCode>
<invoiceCount>1</invoiceCount>
</SendEArchiveDataResult>
</SendEArchiveDataResponse>
</soap:Body>
</soap:Envelope>
However I couldn't reach any of the nodes. I tried this:
foreach($xml->children() as $child) {
echo "Child node: " . $child . "</br>";
}
and it returns empty.
How will I access the nodes ?
Thanks
Simple XML, is - as the name suggests, a very simple implementation and it looks for standard namespaces. You can use registerXPathNamespace to look for non custom ones. See example below that works for your code.
$xml = simplexml_load_string($string);
$xml->registerXPathNamespace("soap", "http://www.w3.org/2003/05/soap-envelope");
print_r($xml->xpath('//soap:Body')[0]->SendEArchiveDataResponse->SendEArchiveDataResult->Invoices->InvoiceStateResult);
This question already has answers here:
Reference - How do I handle Namespaces (Tags and Attributes with a Colon in their Name) in SimpleXML?
(2 answers)
Closed 2 years ago.
my xml not parsing, i have no idea why
First line of xml not parsing, but second line parsing good
I know about im missings whatever in code, but searched in google and not find correct answer for it
// this xml not work, with <soap:Envelope> tags
$string = '<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope 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">
<soap:Body>
<GetBrandsListResponse xmlns="http://tempuri.org/">
<GetBrandsListResult>
<DocumentElement>
<BrandLst>
<ID>1</ID>
<Name>Audi</Name>
</BrandLst>
<BrandLst>
<ID>350</ID>
<Name>BMW</Name>
</BrandLst>
</DocumentElement>
</GetBrandsListResult>
</GetBrandsListResponse>
</soap:Body>
</soap:Envelope>';
// but this xml works, without soap envelope tags
$string = '
<BrandLst>
<ID>1</ID>
<Name>Audi</Name>
</BrandLst>
';
$xml = simplexml_load_string($string);
var_dump($xml);
Fixed with adding xpath and registerXPathNamespace
$xml = simplexml_load_string($string);
$xml->registerXPathNamespace('default', 'http://tempuri.org/');
$auto = $xml->xpath("//default:BrandLst");
This question already has answers here:
Reference - How do I handle Namespaces (Tags and Attributes with a Colon in their Name) in SimpleXML?
(2 answers)
Closed 2 years ago.
now i need to ask this, have this XML:
<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:web="http://webservice.telemetry.udo.fors.ru/" 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">
<soapenv:Header>
<wsse:Security soapenv:mustUnderstand="1">
<wsse:UsernameToken>
<wsse:Username/>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText"/>
</wsse:UsernameToken>
</wsse:Security>
</soapenv:Header>
<soapenv:Body>
<web:storeTelemetryList xmlns="http://webservice.telemetry.udo.fors.ru/">
<telemetryWithDetails xmlns="">
<telemetry>
<coordX>-108.345268</coordX>
<coordY>25.511797</coordY>
<date>2020-04-16T16:48:07Z</date><glonass>0</glonass>
<gpsCode>459971</gpsCode>
<speed>0</speed>
</telemetry>
</telemetryWithDetails>
</web:storeTelemetryList>
</soapenv:Body>
</soapenv:Envelope>
and im using simplexml on PHP to read it but i get the error "Trying to get property 'telemetryWithDetails' of non-object" when i try to get the data in coordx, coordy,date, gpscode and speed node but i cant get there here is my code:
$string = <<<XML
<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope...(same from above)
XML;
$xml = new SimpleXMLElement($string);
echo $xml->Body->storeTelemetryList->telemetryWithDetails;
if i put "->telemetryWithDetails->telemetry->coordX" i get "Trying to get property 'telemetry' of non-object, Trying to get property 'coordX' of non-object" and the same if use "simplexml_load_string" Hope you can help me thanks
A simple solution is to use XPath and describe the "path" to the values you want :
$xml = simplexml_load_string($xmlstring);
$telemetries = $xml->xpath('/soapenv:Envelope/soapenv:Body/web:storeTelemetryList/telemetryWithDetails/telemetry');
$telemetry = $telemetries[0] ;
$coordX = (string) $telemetry->xpath('./coordX')[0] ;
$coordY = (string) $telemetry->xpath('./coordY')[0] ;
echo $coordX ; //-108.345268
echo $coordY ; // 25.511797
XPath returns always a collection, so select the first node with [0]. The (string) conversion is used to extract the text value inside the node.
How can i get errorCode and errorMsg values? I went trough many example but still struggling.
<?xml version="1.0" encoding="utf-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<ns1:doOperationsResponse xmlns:ns1="urn:bulkdeployer.easy.gintel.com" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
<result href="#id0"/>
</ns1:doOperationsResponse>
<multiRef xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:ns2="urn:bulkdeployer.easy.gintel.com" id="id0" soapenc:root="0" soapenv:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xsi:type="ns2:ResponseType">
<errorCode xsi:type="xsd:int">0</errorCode>
<errorMsg xsi:type="xsd:string">No errors</errorMsg>
</multiRef>
</soapenv:Body>
</soapenv:Envelope>
What I have tried:
$xml= simplexml_load_string($content);
$xml->registerXPathNamespace('nam','urn:bulkdeployer.easy.gintel.com'); // need to register namespace
$return_code = $xml->xpath('//nam:errorCode');
$return_msg = $xml->xpath('//nam:errorMsg');
Thanks
This question already has an answer here:
Simplexml get attributes with a namespace
(1 answer)
Closed 8 years ago.
I try to use the Immobilienscout24-API to get Offerings of my Customer to his homepage.
I get an XML-Result with the form like here.
After parsing this result with
$obj = new SimpleXMLElement($myXmlString);
the attribute xsi:type of the element realEstateElement is lost. How can I access that element?
Thanks!
<?php
$xml = <<<XML
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<realestates:realEstates xmlns:ns2="http://rest.immobilienscout24.de/schema/platform/gis/1.0" xmlns:xlink="http://www.w3.org/1999/xlink" xmlns:common="http://rest.immobilienscout24.de/schema/common/1.0" xmlns:realestates="http://rest.immobilienscout24.de/schema/offer/realestates/1.0">
<Paging>
<next xlink:href="http://rest.sandbox-immobilienscout24.de/restapi/api/offer/v1.0/user/me/realestate?pagenumber=2"/>
<pageNumber>1</pageNumber>
<pageSize>20</pageSize>
<numberOfPages>12</numberOfPages>
<numberOfHits>222</numberOfHits>
</Paging>
<realEstateList>
<realEstateElement xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:offerlistelement="http://rest.immobilienscout24.de/schema/offer/listelement/1.0" xsi:type="offerlistelement:OfferHouseRent" xlink:href="http://rest.sandbox-immobilienscout24.de/restapi/api/offer/v1.0/user/me/realestate/62283354" modification="2013-12-05T09:13:45.000+01:00" creation="2013-12-05T09:13:37.000+01:00" publishDate="2013-12-07T20:37:38.333+01:00" id="62283354">
<externalId>62283354</externalId>
<title>Unterbach Haus</title>
<address>
<street>Eichenwand</street>
<houseNumber>2</houseNumber>
<postcode>40627</postcode>
<city>Düsseldorf</city>
<geoHierarchy>
<continent>
<geoCodeId>1</geoCodeId>
<fullGeoCodeId>1</fullGeoCodeId>
</continent>
<country>
<geoCodeId>276</geoCodeId>
<fullGeoCodeId>1276</fullGeoCodeId>
</country>
<region>
<geoCodeId>10</geoCodeId>
<fullGeoCodeId>1276010</fullGeoCodeId>
</region>
<city>
<geoCodeId>12</geoCodeId>
<fullGeoCodeId>1276010012</fullGeoCodeId>
</city>
<quarter>
<geoCodeId>57</geoCodeId>
<fullGeoCodeId>1276010012057</fullGeoCodeId>
</quarter>
<neighbourhood>
<geoCodeId>5111000008401</geoCodeId>
</neighbourhood>
</geoHierarchy>
</address>
<realEstateState>ACTIVE</realEstateState>
<common:publishChannels>
<publishChannel id="10000" title="Immobilienscout24"/>
<publishChannel id="10001" title="Homepage"/>
</common:publishChannels>
<price>
<value>45444</value>
<currency>EUR</currency>
</price>
<livingSpace>440</livingSpace>
<plotArea>460</plotArea>
<numberOfRooms>5</numberOfRooms>
<builtInKitchen>false</builtInKitchen>
</realEstateElement>
</realEstateList>
</realestates:realEstates>
XML;
$sxml = new SimpleXMLElement($xml);
var_dump($sxml->realEstateList->realEstateElement->attributes('xsi', true)->type);
http://www.php.net/manual/en/simplexmlelement.attributes.php Comments might be useful ;)