how to add prefix and URI on xml objects (PHP) - php

in this sample found here:
<?php
$newsXML = new SimpleXMLElement("<news></news>");
$newsXML->addAttribute('newsPagePrefix', 'value goes here');
$newsIntro = $newsXML->addChild('content');
$newsIntro->addAttribute('type', 'latest');
Header('Content-type: text/xml');
echo $newsXML->asXML();
?>
We create an xml object from scratch. all righ.
my question is, how to embed a prefix in a tag during the construction of the object?
<?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:Header />
<soapenv:Body>
<EnviarLoteRpsEnvio xmlns="http://www.betha.com.br/e-nota-contribuinte-ws">
...some important xml...
</EnviarLoteRpsEnvio>
</soapenv:Body>
</soapenv:Envelope>
in this code above, show a final xml, how, during the create of xml object, i embed the prefixes in the tags? sorry for my bugged english..
thanks for all help.

Related

Read XML data from string [duplicate]

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.

SOAP XML inside XML - How to parse with PHP

I've searched in the SO, but nothing what i've found could help me.
I'm doing system integration with JadLog, a freight service.
When I pass the query with the product and delivery variables, an XML is returned.
Return example:
<?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>
<valorarResponse xmlns="">
<ns1:valorarReturn xmlns:ns1="http://jadlogEdiws">
<?xml version="1.0" encoding="utf-8" ?>
<string xmlns="http://www.jadlog.com.br/JadlogEdiWs/services">
<Jadlog_Valor_Frete>
<versao>1.0</versao>
<Retorno>1458,62</Retorno>
<Mensagem>Valor do Frete</Mensagem>
</Jadlog_Valor_Frete>
</string>
</ns1:valorarReturn>
</valorarResponse>
</soapenv:Body>
</soapenv:Envelope>
So, there is more than one XML declaration, right?
The only value that I need is the value of the freight, wich is in the tag Retorno. I this case, 1458,62.
What am I trying to do:
$your_xml_response = file_get_contents($url_project);
$clean_xml = str_replace('soapenv:', '', $your_xml_response);
$xml = simplexml_load_string($clean_xml);
var_dump($xml);
What its returns:
object(SimpleXMLElement)[1]
public 'Body' =>
object(SimpleXMLElement)[2]
public 'valorarResponse' =>
object(SimpleXMLElement)[3]
If I try to echo $xml->Retorno, it Returns empty.
How can I get the value of tag Retorno?
When I tried to load your XML string with simplexml_load_string it causes me
There is more than one XML declaration which is not allowed in xml . That's why I used regular express to get Retorno value. May be its not the best approach to use regex on XML but you can make a try.
<?php
$re = '/<Retorno>(.*?)<\/Retorno>/m';
$str = '<?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>
<valorarResponse xmlns="">
<ns1:valorarReturn xmlns:ns1="http://jadlogEdiws">
<?xml version="1.0" encoding="utf-8" ?>
<string xmlns="http://www.jadlog.com.br/JadlogEdiWs/services">
<Jadlog_Valor_Frete>
<versao>1.0</versao>
<Retorno>1458,62</Retorno>
<Mensagem>Valor do Frete</Mensagem>
</Jadlog_Valor_Frete>
</string>
</ns1:valorarReturn>
</valorarResponse>
</soapenv:Body>
</soapenv:Envelope>
';
preg_match_all($re, $str, $matches);
//debugging
print '<pre>';
print_r($matches);
print '<pre>';
//result
echo $matches[0][0];

Read Array Object from WSDL XML with namespaces in PHP

I am sending array of objects in XML Soap response from my java code to php in my project using WSDL.
I want to store values in php for my project.
I tried many ways but couldn't able to find how to parse my xml and read values. I am not export in xml area.
Please anyone help me for read my values from values.
My SOAP Response Body:
<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>
<getactualtimerequestresponse xmlns="http://impl.sample.com">
<getactualtimereturn>.
<ns1:projectlist xmlns:ns1="http://response.sample.com">
<item>
<ns2:userid xmlns:ns2="http://request.sample.com">4</ns2:userid>
<ns3:username xmlns:ns3="http://request.sample.com">Manoj Arun</ns3:username>
</item>
<item>
<ns5:userid xmlns:ns5="http://request.sample.com">5</ns5:userid>
<ns6:username xmlns:ns6="http://request.sample.com">Sethu Raman</ns6:username>
</item>
</ns1:projectlist>
<ns10:message xsi:nil="true" xmlns:ns10="http://response.sample.com"></ns10:message>
</getactualtimereturn>
</getactualtimerequestresponse>
</soapenv:body>
</soapenv:envelope>
projectList is my object created in java.
In PHP:
I tried to read like below but i didn't got anything.
foreach($xml->xpath('//ns:projectList') as $header)
{
foreach($header->item as $userIds)
{
echo $userIds->xpath('//ns:userId');
}
}
Thanks in advance...
$client = new SoapClient('http://url.com?wsdl');
$params = array(Java arg => php value);
$result = $client->Function( $parms );
print_r($result);

How to get values from xml output using PHP

How to get values from below result using PHP.
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Header/>
<soapenv:Body>
<p558:registerDonorResponse xmlns:p558="http://ws.ots.labcorp.com">
<p558:registerDonorReturn xmlns:p118="http://data.ws.ots.labcorp.com">
<p118:clientRegistrationId>clr1</p118:clientRegistrationId>
<p118:labcorpRegistrationNumber>100059064</p118:labcorpRegistrationNumber>
<p118:registrationTime>2012-12-01T05:40:51.628Z</p118:registrationTime>
</p558:registerDonorReturn>
</p558:registerDonorResponse>
</soapenv:Body>
</soapenv:Envelope>
Thanks.
Your XML contains namespace-prefixed tags as it's common for SOAP responses.
Have a look at following comment from php's SimpleXML docs:
$xml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<people xmlns:p="http://example.org/ns" xmlns:t="http://example.org/test">
<p:person id="1">John Doe</p:person>
<p:person id="2">Susie Q. Public</p:person>
</people>
XML;
$sxe = new SimpleXMLElement($xml);
$ns = $sxe->getNamespaces(true);
$child = $sxe->children($ns['p']);
foreach ($child->person as $out_ns)
{
echo $out_ns;
}
In your case code accessing the properties should look like that (it's tested against your XML in so.xml file):
<?php
$xml = file_get_contents('so.xml');
$sxe = simplexml_load_string($xml);
$ns = $sxe->getNamespaces(true);
$child =
$sxe->children($ns['soapenv'])->
Body->children($ns['p558'])->
registerDonorResponse->registerDonorReturn->children($ns['p118']);
var_dump($child);
Result:
$ php -f so.php
object(SimpleXMLElement)#4 (3) {
["clientRegistrationId"]=>
string(4) "clr1"
["labcorpRegistrationNumber"]=>
string(9) "100059064"
["registrationTime"]=>
string(24) "2012-12-01T05:40:51.628Z"
}
Please note however that issuing SOAP requests and parsing responses by hand is generally a bad practice, consider using SOAP client for that.
What have you tried?
Anyway you can use PHP's simplexml_load_file or the full featured DOMDocument class

Extract values from soapenv envelope xml in php

i am new to XMl. i want to extract the status values in the following xml .i have no idea how to do that in php . this is a response that i ma getting from a API call.
<soapenv:envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:body>
<savesalesorderresponse xmlns="http://www.smartturn.com/services/OccamService/sales-order">
<uploadresponse>
<ns1:externalrefid xmlns:ns1="http://www.smartturn.com/services/occamtypes">007</ns1:externalrefid>
<ns2:status xmlns:ns2="http://www.smartturn.com/services/occamtypes">SUCCESS</ns2:status>
<ns6:systemid xmlns:ns6="http://www.smartturn.com/services/occamtypes">SO-059241</ns6:systemid>
</uploadresponse>
</savesalesorderresponse>
</soapenv:body>
</soapenv:envelope>
solution code will be appreciated
thanks in advance
All you need to do is Register namespaces with registerXPathNamespace
$xml = new SimpleXMLElement($data);
$xml->registerXPathNamespace("ns", "http://www.smartturn.com/services/occamtypes");
$status = $xml->xpath('//ns:status');
$status = (string) $status[0];
print($status);
Output
SUCCESS
The easiest way is to use SimpleXML

Categories