I have the file.xml contains:
<?xml version="1.0"?>
<Document xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:iso:std:iso:20022:tech:xsd:test.001.001.01">
<book>
<author>Jack Herrington</author>
<title>PHP Hacks</title>
<publisher>O'Reilly</publisher>
</book>
</Document>
anybody know How Can I get this information in the <document....> example the xmlns?
<Document xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:iso:std:iso:20022:tech:xsd:test.001.001.01">
Use SimpleXML to get namespaces like so:
$xmlstr = <<<XML
<?xml version="1.0"?>
<Document xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="urn:iso:std:iso:20022:tech:xsd:test.001.001.01">
<book>
<author>Jack Herrington</author>
<title>PHP Hacks</title>
<publisher>O'Reilly</publisher>
</book>
</Document>
XML;
$xml = simplexml_load_string($xmlstr);
$namespaces = $xml->getDocNamespaces();
var_export($namespaces);
this code will produce:
array ( 'xsd' => 'http://www.w3.org/2001/XMLSchema', 'xsi' => 'http://www.w3.org/2001/XMLSchema-instance', '' => 'urn:iso:std:iso:20022:tech:xsd:test.001.001.01', )
The array of all namespaces declared in the document.
Related
<?xml version="1.0" encoding="ISO-8859-1"?>
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:getRatesResponse xmlns:ns1="urn:rpxwsdl">
<return xsi:type="xsd:string">{"RPX":{"TITLE":"Rates From Jakarta (JAK) TO Denpasar (DPS), Weight 1 Kg","DATA":[{"SERVICE":"PAS Reguler (PSR)","FREIGHT_CHARGE":"27723","TOT_CHARGE":"27723","PRICE":"27723","DISCOUNT":"0","ETF":"N\/A","ETD":"N\/A"},{"SERVICE":"","FREIGHT_CHARGE":"0","TOT_CHARGE":"0","PRICE":"0","DISCOUNT":"0","ETF":"0","ETD":"0"}]}}</return>
</ns1:getRatesResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
I have a json inside an xml response. however how do i retrieve it from the response.
Use the DOM classes to get the relevant text content. This will handle the entities. Then you can simply use json_decode.
For example (demo):
<?php
declare(strict_types=1);
$xml = <<<XML
<?xml version="1.0" encoding="ISO-8859-1"?>
<SOAP-ENV:Envelope SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/">
<SOAP-ENV:Body>
<ns1:getRatesResponse xmlns:ns1="urn:rpxwsdl">
<return xsi:type="xsd:string">{"RPX":{"TITLE":"Rates From Jakarta (JAK) TO Denpasar (DPS), Weight 1 Kg","DATA":[{"SERVICE":"PAS Reguler (PSR)","FREIGHT_CHARGE":"27723","TOT_CHARGE":"27723","PRICE":"27723","DISCOUNT":"0","ETF":"N\/A","ETD":"N\/A"},{"SERVICE":"","FREIGHT_CHARGE":"0","TOT_CHARGE":"0","PRICE":"0","DISCOUNT":"0","ETF":"0","ETD":"0"}]}}</return>
</ns1:getRatesResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
XML;
$doc = new DOMDocument('1.0', 'ISO-8859-1');
$doc->loadXML($xml);
$xpath = new DOMXPath($doc);
$xpath->registerNamespace('ns1', 'urn:rpxwsdl');
$item = $xpath->query('/SOAP-ENV:Envelope/SOAP-ENV:Body/ns1:getRatesResponse/return');
if ($item->length > 0) {
print_r(
json_decode($item->item(0)->textContent, true, 512, JSON_THROW_ON_ERROR)
);
}
I get different XML strings via SOAP.
But it is very difficult for me to get the value from XML with PHP.
XML examples:
<?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>
<GetUserInfoResponse xmlns="http://schemas.microsoft.com/sharepoint/soap/directory/">
<GetUserInfoResult>
<GetUserInfo>
<User ID="23" />
</GetUserInfo>
</GetUserInfoResult>
</GetUserInfoResponse>
</soap:Body>
</soap:Envelope>
<?xml version = "1.0" encoding = "utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<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>
<z:row ows_ID="128" />
</rs:data>
</listitems>
</GetListItemsResult>
</GetListItemsResponse>
</soap:Body>
</soap:Envelope>
I would like to get the id.
I tried it like this:
$xml_element = simplexml_load_string($responseContent);
$name_spaces = $xml_element->getNamespaces(true);
$soap = $xml_element->children($name_spaces['soap'])
->Body
->children($name_spaces['rs'])
->GetListItemsResponse
->GetListItemsResult
->listitems
->{'rs:data'}
->{'z:row'}['ows_ID'][0];
But most time I dont know how to get my value.
Is it possible to display a whole array or how do I get the path to the value?
What you could do to get the value of 'ows_ID' is to use the SimpleXMLElement children method and also add the namespaces for the child elements.
You can use the attributes method to get the value for 'ows_ID';
For example:
$responseContent = <<<XML
<?xml version = "1.0" encoding = "utf-8"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<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>
<z:row ows_ID="128" />
</rs:data>
</listitems>
</GetListItemsResult>
</GetListItemsResponse>
</soap:Body>
</soap:Envelope>
XML;
$xml_element = simplexml_load_string($responseContent);
$name_spaces = $xml_element->getNamespaces(true);
$rows = $xml_element
->children($name_spaces['soap'])
->Body
->children()
->GetListItemsResponse
->GetListItemsResult
->listitems
->children($name_spaces['rs'])
->children($name_spaces['z']);
foreach ($rows as $row) {
$ows_ID = $row->attributes()->ows_ID;
}
Demo
Hi I need to modify xml using php from exsiting xml content
existing xml
<?xml version="1.0" encoding="utf-8"?>
<Book xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" BookId="10010">
<STRING id="Name" >Test1</STRING>
<STRING id="ISBN">102399</STRING>
</Book>
new xml
<?xml version="1.0" encoding="utf-8"?>
<Book BookId="10010" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" >
<STRING id="Name" >XYZ</STRING>
<STRING id="ISBN">7777</STRING>
</Book>
Note: BookID should be place before xmlns attribute
Thank you in advance.
Use SimpleXML:
$xml = simplexml_load_file($yourXMLPath);
//Loop for element...
foreach ($xml->STRING as $string) {
//Update value for <STRING id="Name">...
if($string->attributes()->id == 'Name'){
$xml->STRING = 'XYZ';
}
//Update value for <STRING id="Name">...
if($string->attributes()->id == 'ISBN'){
$xml->STRING = '7777';
}
}
// save the updated document
$xml->asXML('newXML.xml');
I am wondering why the below code doesn't show the value of processResponse tag while it shows the whole XML and the Body tag.
This is the XML which I am handling
$xml = '<?xml version="1.0"?>
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wsa="http://www.w3.org/2005/08/addressing">
<env:Header>
<wsa:MessageID>urn:df1231asfer5e4564affds</wsa:MessageID>
<wsa:ReplyTo><wsa:Address>http://www.w3.org/2005/08/addressing/anonymous</wsa:Address></wsa:ReplyTo>
</env:Header>
<env:Body>
<processResponse xmlns="http://xmlns.oracle.com/EligibilityProcess/EligibilityProcess/EligibilityBPEL">
<generatedMessageRefNo>451</generatedMessageRefNo>
<providerRefNo>41</providerRefNo>
<tpaRequestId>4184612387</tpaRequestId>
<contractHolder>Rami Zbeeb</contractHolder>
<contractNo>81456954</contractNo>
<guarantorName>ANC</guarantorName>
<eligibilityStatus>Success</eligibilityStatus>
<eligibilityReason xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
<messageOrNotes xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
<patientShare></patientShare>
<consentForm></consentForm>
<webServTechStatus>Success</webServTechStatus>
<replyCode xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
<replyDescription xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true"/>
</processResponse></env:Body></env:Envelope>';
I am using the SimpleXML class:
$res = new SimpleXMLElement($xml);
When I am showing the body XML it works:
$str = $res->children('env',true)->Body->asXML();
echo "<pre>",htmlentities($str),"</pre>";
However when showing the processResponse XML or string it doesn't work:
$str = $res->children('env',true)->Body->processResponse->asXML();
echo "<pre>",htmlentities($str),"</pre>";
Kindly advice.
You can get the children of Body to get the processResponse:
$str = $res->children('env',true)->Body->children()->processResponse->asXML();
echo "<pre>",htmlentities($str),"</pre>";
I'm working on some project and i have to save movie(id,title,genre) in a xml file using php.
when i create xml file and add information it works perfectly
I mean ,
file : php
$document_xml = new DomDocument("1.0","UTF-8");
$movie_dom = $document_xml->createElement("movie");
$id_dom = $document_xml->createElement("id",$id);
$title_dom = $document_xml->createElement("title",$title);
$genre_dom = $document_xml->createElement("genre",$genre);
$movie_dom->appendChild($id_dom);
$movie_dom->appendChild($title_dom);
$movie_dom->appendChild($genre_dom);
$document_xml->appendChild($movie_dom);
$document_xml->save("data.xml");
and i have
file : data.xml
<?xml version="1.0" encoding="UTF-8"?>
<movie>
<id>1</id>
<title>Iron man</title>
<genre>Action</genre>
</movie>
but when i modify the code like this
file : php
$document_xml = new DomDocument();
$document_xml->load("data.xml");
$movie_dom = $document_xml->createElement("movie");
$id_dom = $document_xml->createElement("id",$id);
$title_dom = $document_xml->createElement("title",$title);
$genre_dom = $document_xml->createElement("genre",$genre);
$movie_dom->appendChild($id_dom);
$movie_dom->appendChild($title_dom);
$movie_dom->appendChild($genre_dom);
$document_xml->appendChild($movie_dom);
$document_xml->saveXML();
so that to have,
<?xml version="1.0" encoding="UTF-8"?>
<movie>
<id>1</id>
<title>Iron man</title>
<genre>Action</genre>
</movie>
<movie>
<id>id</id>
<title>some title</title>
<genre>some genre</genre>
</movie>
I have no error and there is no change in my xml file
I would like your help
I found solution, i change the structure of my xml file
<?xml version="1.0" encoding="UTF-8"?>
<data>
<movie>
<id>1</id>
<title>Iron man</title>
<genre>Action</genre>
</movie>
<movie>
<id>2</id>
<title>La guerre des etoiles</title>
<genre>adventure</genre>
</movie>
<movie>
<id>3</id>
<title>Jugement dernier</title>
<genre>action</genre>
</movie>
</data>
i change my php file like this
$document_xml = new DomDocument();
$document_xml->load("data.xml",LIBXML_NOBLANKS);
$document_xml->formatOutput = true;
$root = $document_xml->documentElement;
$movie_dom = $document_xml->createElement("movie");
$id_dom = $document_xml->createElement("id",$value);
$title_dom = $document_xml->createElement("title",$title);
$genre_dom = $document_xml->createElement("genre",$genre);
$movie_dom->appendChild($id_dom);
$movie_dom->appendChild($title_dom);
$movie_dom->appendChild($genre_dom);
$root->appendChild($movie_dom);
$document_xml->appendChild($root);
$document_xml->save("data.xml");
everything wworks perfectfly ...