Parsing XML with namespace information [duplicate] - php

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"}; ?>

Related

Get XML fields after SOAP call [duplicate]

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.

php simplxml - Trying to get property of non-object in [duplicate]

This question already has an answer here:
Trying to get property of non-object SimpleXML?
(1 answer)
Closed 8 years ago.
So I'm getting an error trying to access player name in an XML File using the code. I'm not sure if i'm accessing the information right is there something I'm doing wrong?
<?php
$xml = simplexml_load_file('trueskill.bcn');
$playername = $xml->SK92->Players[0]->Name;
echo $playername;
?>
Using the XML File
<?xml version="1.0" encoding="utf-8"?>
<SK92>
<Settings Multiplier="200" Decay="0" />
<Players>
<Player Name="Mark" Team="" Invisible="false" Characters="" Alts="JFG;Mark;SEBA" />
<Player Name="Antonio" Team="" Invisible="false" Characters="" Alts="Proxy;Toni" />
</Players>
</SK92>
echo $xml->Players->Player['Name'];
should do the trick.
To have it in variables:
$playername = $xml->Players->Player['Name'];
echo $playername;
SIDENOTE:
As I suppose you want to access all the players, you can do this by:
foreach ($xml->Players->Player as $play) {
echo $play['Name'] . "<br>\n";
}

Why does SimpleXML XPath return an empty array? [duplicate]

This question already has an answer here:
PHP simplexml: why does xpath stop working?
(1 answer)
Closed 8 years ago.
I've the following XML-File:
<SyncCustomerPartyMaster xmlns="http://schema.infor.com/InforOAGIS/2" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xsi:schemaLocation="http://schema.infor.com/InforOAGIS/2 http://schema.infor.com/2.9.1/InforOAGIS/BODs/Developer/SyncCustomerPartyMaster.xsd" releaseID="9.2" versionID="2.9.1">
<ApplicationArea>
<Sender>
<LogicalID>lid://infor.ln.dach_nausveln1_200</LogicalID>
<ComponentID>erp</ComponentID>
<ConfirmationCode>OnError</ConfirmationCode>
</Sender>
<CreationDateTime>2014-10-09T13:47:48Z</CreationDateTime>
</ApplicationArea>
<DataArea>
<Sync>
<TenantID>infor</TenantID>
<AccountingEntityID>200</AccountingEntityID>
<LocationID/>
<ActionCriteria>
<ActionExpression actionCode="Add"/>
</ActionCriteria>
</Sync>
<CustomerPartyMaster>
<PartyIDs>
<ID accountingEntity="200" lid="lid://infor.ln.dach_nausveln1_200" variationID="108">10100</ID>
<DisplayID>10100</DisplayID>
</PartyIDs>
</CustomerPartyMaster>
</DataArea>
</SyncCustomerPartyMaster>
And I'm trying to access the DisplayId via XPath: '/SyncCustomerPartyMaster/DataArea/CustomerPartyMaster/PartyIDs/DisplayID'
<?php
$xml = simplexml_load_file("example.xml");
$displayId = $xml->xpath('/SyncCustomerPartyMaster/DataArea/CustomerPartyMaster/PartyIDs/DisplayID');
print_r($xml);
echo '<br />';
echo '<br />';
print_r($displayId);
?>
But print_r($display_id) just returns an empty array. I fiddled around a bit with registerXPathNamespace]1 which did not do the trick - can anyone please point me into the right direction?
xmlns="http://schema.infor.com/InforOAGIS/2"
This declares a default namespace which you do need to register via registerXPathNamespace() and address the elements using the prefix registered.
Example:
$xml = simplexml_load_file("example.xml");
$xml->registerXPathNamespace('ns', "http://schema.infor.com/InforOAGIS/2");
$displayIds = $xml->xpath('/ns:SyncCustomerPartyMaster/ns:DataArea/ns:CustomerPartyMaster/ns:PartyIDs/ns:DisplayID');
foreach ($displayIds as $displayId) {
echo $displayId;
}
Output:
10100

PHP Script to generate XML with namespaces and attributes [duplicate]

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

Parse SOAP response with namespaces in PHP [duplicate]

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

Categories