This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 3 years ago.
My asmx WEB service return this XML
<?xml version="1.0" encoding="ISO-8859-1"?>
<PRODUCT>
<DESC>Vanilla ice cream</DESC>
<CODEERR>0</CODEERR>
</PRODUCT>
Calling WEB service from this PHP code
$SoapCli = new SoapClient('http://www.foo.com/MyService.asmx?WSDL');
$params = array(
'PARAM1' => 'some_param_1',
'PARAM2' => 'some_param_2',
);
$resp_WS = $SoapCli->__soapCall('MyFunction', array($params));
var_dump($resp_WS);
result is
object(stdClass)#11946 (1) {
["MyFunctionResult"]=>
object(stdClass)#11947 (1) {
["any"]=>
string(88) "<product xmlns=""><desc>Vanilla ice cream</desc><codeerr>0</codeerr></product>"
}
}
but, after googling a lot, I don't find PHP code for retreive values of two fields DESC and CODER
You can use json_encode,json_decode,simplexml_load_string to parse the XML response, try the following code snippet to read the XML response
$xml = '<?xml version="1.0" encoding="ISO-8859-1"?>
<PRODUCT>
<DESC>Vanilla ice cream</DESC>
<CODEERR>0</CODEERR>
</PRODUCT>';
$res = json_decode(json_encode((array)simplexml_load_string($xml)),true);
Now you can use $res['DESC'] and $res['CODEERR'] to retrive the values.
Related
This question already has answers here:
How to convert XML attributes to text nodes
(3 answers)
Closed 7 years ago.
I want to write a PHP script that will modify my XML file.
I have my productId within the node as an attribute and I want to parse the entire file and convert it to a separate node. So I want to read the attribute of the node and put that attribute in its own node. But the rest of the nodes will stay as is.
Before:
<product id="123">
<name>bob</name>
<lastname>tim</lastname>
</product>
To:
<product>
<id>123</id>
<name>bob</name>
<lastname>tim</lastname>
</product>
Can I do this in PHP? Bearing in mind the file will have over one thousand separate products in it.
You could do it this way.
$xml = new SimpleXMLElement('<product id="123"></product>');
if(!empty($xml['id'])) {
$xml->addChild('id', $xml['id']);
unset($xml['id']);
}
echo $xml->asXML();
Output:
<?xml version="1.0"?>
<product><id>123</id></product>
Here's the manual's link and the addchild functions link. http://php.net/manual/en/class.simplexmlelement.php
http://php.net/manual/en/simplexmlelement.addchild.php
Update:
If you had multiple products you could loop like this.
$xml = new SimpleXMLElement('<proudcts><product id="123"></product><product id="234"></product></proudcts>');
foreach($xml as $key => $data){
if(!empty($data['id'])) {
$data->addChild('id', $data['id']);
unset($data['id']);
}
}
echo $xml->asXML();
Output:
<?xml version="1.0"?>
<proudcts><product><id>123</id></product><product><id>234</id></product></proudcts>
This question already has answers here:
How do you parse and process HTML/XML in PHP?
(31 answers)
Closed 8 years ago.
Greetings Everybody & thanks in advance for your answers.
So, my difficulty is this, i found a nice tutorial about a restful web service and i can not manage to modify it to my needs.
The below sample code works like a charm
function get_price($find) {
$products = array(
"test" => 293,
"test2" => 348,
"test3" => 267
);
foreach ($products as $product => $price) {
if ($product == $find) {
return $price;
break;
}}}
The $price return runs correctly for each of the above elements of the array which i choose through a simple html form.
Now my code
function get_tickets($find){
$xml = simplexml_load_file("products.xml");
// Here i want to return some of the xml's elements
}
and the included xml file
<products>
<company>
<name>test</name>
<tel>test</tel>
</company>
<products>
<product id="1">
<name>test</name>
<value>test</value>
<color>test</color>
</product>
</products>
</products>
I want to be able to access all the elements of the xml like the first sample code. Thank you.
The xpath() function should do want you want. It returns an array of elements that match the path given. For example, $xml->xpath("products/products/product") will give you all your product elements.
This question already has an answer here:
Create XML with xmlns:xlink attribute in a node
(1 answer)
Closed 8 years ago.
Thanks for your help, I need php script to generate the following XML
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<design xmlns:xlink="http://www.w3.org/1999/xlink" xmlns="http://anydomain.com">
<name>xxx</name>
<description>yyy</description>
</design>
You could use SimpleXML to create such xml.
Rough example:
$xml = new SimpleXMLElement('<design />'); // set parent node
$xml->addAttribute('xmlns', 'http://anydomain.com'); // attributes
$xml->addAttribute('xlink:ns', '', 'http://www.w3.org/1999/xlink');
unset($xml->attributes('xlink', true)['ns']);
$xml->addChild('name', 'xxx'); // add those children
$xml->addChild('description', 'yyy');
echo $xml->asXML(); // output
This question already has answers here:
SimpleXML SOAP response Namespace issues
(2 answers)
Closed 8 years ago.
I found lot of solutions for this problem, but my code won't work!
XML INFO:
<?xml version="1.0" encoding="UTF-8"?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
<S:Body>
<ns2:resultado xmlns:ns2="http://webservice.consulta.spcjava.spcbrasil.org/" data="2014-06-03T11:37:32.001-03:00" restricao="false">
<protocolo digito="2" numero="1204248496" />
.... other XML info
MY CODE:
$s = '<?xml version="1.0" encoding="UTF-8"?><S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/"><S:Body><ns2:resultado xmlns:ns2="http://webservice.consulta.spcjava.spcbrasil.org/" data="2014-06-03T11:37:32.001-03:00" restricao="false"><protocolo digito="2" numero="1204248496" /> ...
$xml = simplexml_load_string($s);
$x2 = $xml->children('http://schemas.xmlsoap.org/soap/envelope/')->Body->children('http://webservice.consulta.spcjava.spcbrasil.org/')->resultado->protocolo->digito;
print_r($x2);
var_dump(count($x2));
Returns null and 0 for the object count.
I've been following this tutorial: http://amigotechnotes.wordpress.com/2013/11/16/parse-xml-with-namespace-by-simplexml-in-php/
I don't get where my example differs from his example. :/
Can anyone help me with this issue, please?
Finally made it work using another syntax.
$xml = simplexml_load_string($resultadoDocumento[0]["cdo_xml"]);
$resultadoSPC = $xml->children('S', TRUE)->Body->children('ns2', TRUE)->children();
accessing nodes by:
<?php foreach ($resultadoSPC->consumidor->children()->{"consumidor-pessoa-fisica"} as $consumidorElement) : ?>
<?php echo $consumidorElement->attributes()->{"nome"}; ?>
This question already has answers here:
parse an XML with SimpleXML which has multiple namespaces [duplicate]
(5 answers)
Closed 8 years ago.
Any recommendations how to parse this SOAP response and obtain the value of name for the report_type? Notice there are two instances of name; one under report_type and the other under severity.
Here is the SOAP response
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<ns1:getIRResponse xmlns:ns1="http://ws.icontent.idefense.com/V3/2">
<ns1:return xsi:type="ns1:IRResponse" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<ns1:report_type>
<ns1:id>0</ns1:id>
<ns1:name>Original Vulnerability</ns1:name>
</ns1:report_type>
<ns1:severity>
<ns1:id>0</ns1:id>
<ns1:name>HIGH</ns1:name>
</ns1:severity>
</ns:return>
</ns1:getIRResponse>
</soapenv:Body>
</soapenv:Envelope>
Here is the PHP code I'm using:
<?php
$xml = simplexml_load_string($response);
$xml->registerXPathNamespace('ns1', 'http://ws.icontent.idefense.com/V3/2');
foreach ($xml->xpath('//ns1:report_type/name') as $item)
{
echo 'Name: '.$item,'<br>';
}
?>
The PHP code doesn't echo anything. When I use ($xml->xpath('//ns1:name') as $item) it returns both names (Original Vulnerability and HIGH).
I know I'm missing something stupid. Can you help please? Thank you in advance!
Firstly, I've corrected this element
</ns:return>
and changed it to
</ns1:return>
I seem to get the result you're after by duplicating the namespace prefix in both xpath segments
$xml = simplexml_load_string($response);
$xml->registerXPathNamespace('ns1','http://ws.icontent.idefense.com/V3/2');
foreach ($xml->xpath('//ns1:report_type/ns1:name') as $item)
{
echo 'Name: '.$item,'<br>';
}
output
Name: Original Vulnerability