I read other posts and solutions, and they don't work for me - or perhaps I'm not understanding them well enough.
I have a hp network scanner, and have a perl script that interacts through a series of transactions such that I can initiate a scan. I'm working to port that rather directly to php; more suitable for the server I want to run it on. Some transactions work, some don't. This is about one that doesn't.
I took the XML from one of the queries and it won't successfully parse (or this is where I don't understand it well enough). I'm running php version 7.1.12, in case there is something related to that.
my test outputs this:
> php xmltest.php
SimpleXMLElement Object
(
)
object(SimpleXMLElement)#1 (0) {
}
>
And if the xml is simpler (no name-space info I think), then the print_r() is quite verbose.
And here is the full test script with some actual data to process
error_reporting( E_ALL );
ini_set('display_errors', 1);
$test_1 = <<<EOM
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope"
xmlns:SOAP-ENC="http://www.w3.org/2003/05/soap-encoding"
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:wst="http://schemas.xmlsoap.org/ws/2004/09/transfer"
xmlns:mex="http://schemas.xmlsoap.org/ws/2004/09/mex"
xmlns:wsdp="http://schemas.xmlsoap.org/ws/2006/02/devprof"
xmlns:PNPX="http://schemas.microsoft.com/windows/pnpx/2005/10"
xmlns:UNS1="http://www.microsoft.com/windows/test/testdevice/11/2005"
xmlns:dd="http://www.hp.com/schemas/imaging/con/dictionaries/1.0"
xmlns:wprt="http://schemas.microsoft.com/windows/2006/08/wdp/print"
xmlns:wscn="http://schemas.microsoft.com/windows/2006/08/wdp/scan">
<SOAP-ENV:Header>
<wsa:To>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:To>
<wsa:Action>http://schemas.xmlsoap.org/ws/2004/09/transfer/GetResponse</wsa:Action>
<wsa:MessageID>urn:uuid:fec6e42d-5356-1f69-9c3a-001f2927cf33</wsa:MessageID>
<wsa:RelatesTo>urn:uuid:704ccde5-6861-415d-bd65-31dd9d7a8b98</wsa:RelatesTo>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<mex:Metadata>
<mex:MetadataSection Dialect="http://schemas.xmlsoap.org/ws/2006/02/devprof/ThisDevice">
<wsdp:ThisDevice>
<wsdp:FriendlyName xml:lang="en">Printer (HP Color LaserJet CM1312nfi MFP)</wsdp:FriendlyName>
<wsdp:FirmwareVersion>20140625</wsdp:FirmwareVersion>
<wsdp:SerialNumber>CNB885H665</wsdp:SerialNumber>
</wsdp:ThisDevice>
</mex:MetadataSection>
<mex:MetadataSection Dialect="http://schemas.xmlsoap.org/ws/2006/02/devprof/ThisModel">
<wsdp:ThisModel>
<wsdp:Manufacturer xml:lang="en">HP</wsdp:Manufacturer>
<wsdp:ManufacturerUrl>http://www.hp.com/</wsdp:ManufacturerUrl>
<wsdp:ModelName xml:lang="en">HP Color LaserJet CM1312nfi MFP</wsdp:ModelName>
<wsdp:ModelNumber>CM1312nfi MFP</wsdp:ModelNumber>
<wsdp:PresentationUrl>http://192.168.1.20:80/</wsdp:PresentationUrl>
<PNPX:DeviceCategory>Printers</PNPX:DeviceCategory>
</wsdp:ThisModel>
</mex:MetadataSection>
<mex:MetadataSection Dialect="http://schemas.xmlsoap.org/ws/2006/02/devprof/Relationship">
<wsdp:Relationship Type="http://schemas.xmlsoap.org/ws/2006/02/devprof/host">
<wsdp:Hosted>
<wsa:EndpointReference>
<wsa:Address>http://192.168.1.20:3910/</wsa:Address>
<wsa:ReferenceProperties>
<UNS1:ServiceIdentifier>uri:prn</UNS1:ServiceIdentifier>
</wsa:ReferenceProperties>
</wsa:EndpointReference>
<wsdp:Types>wprt:PrinterServiceType</wsdp:Types>
<wsdp:ServiceId>uri:1cd4F16e-7c8a-a7a0-3797-00145a8827ce</wsdp:ServiceId>
<PNPX:CompatibleId>http://schemas.microsoft.com/windows/2006/08/wdp/print/PrinterServiceType</PNPX:CompatibleId>
</wsdp:Hosted>
</wsdp:Relationship>
</mex:MetadataSection>
</mex:Metadata>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
EOM;
$myxml1 = simplexml_load_string($test_1);
print_r($myxml1);
var_dump($myxml1);
exit;
?>
There are several parameters nestled in there that I want to pull out. One, for instance is:
<wsa:Address>http://192.168.1.20:3910/</wsa:Address>
Can you help me close my knowledge gap on how to access this parameter?
thanks!
First of all, soap and namespaces just make parsing XML harder than it has to be. I've never parsed XML that had namespaces that actually made the XML better to understand, or had any benefit at all. I fully get why namespaces exist, but it just means jumping through some extra hoops to get the data out. The trick with namespaces is that you have to "enter in" to the namespace branch by asking that the namespace as a child.
<?php
error_reporting( E_ALL );
ini_set('display_errors', 1);
$str = <<<EOM
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://www.w3.org/2003/05/soap-envelope"
xmlns:SOAP-ENC="http://www.w3.org/2003/05/soap-encoding"
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:wst="http://schemas.xmlsoap.org/ws/2004/09/transfer"
xmlns:mex="http://schemas.xmlsoap.org/ws/2004/09/mex"
xmlns:wsdp="http://schemas.xmlsoap.org/ws/2006/02/devprof"
xmlns:PNPX="http://schemas.microsoft.com/windows/pnpx/2005/10"
xmlns:UNS1="http://www.microsoft.com/windows/test/testdevice/11/2005"
xmlns:dd="http://www.hp.com/schemas/imaging/con/dictionaries/1.0"
xmlns:wprt="http://schemas.microsoft.com/windows/2006/08/wdp/print"
xmlns:wscn="http://schemas.microsoft.com/windows/2006/08/wdp/scan">
<SOAP-ENV:Header>
<wsa:To>http://schemas.xmlsoap.org/ws/2004/08/addressing/role/anonymous</wsa:To>
<wsa:Action>http://schemas.xmlsoap.org/ws/2004/09/transfer/GetResponse</wsa:Action>
<wsa:MessageID>urn:uuid:fec6e42d-5356-1f69-9c3a-001f2927cf33</wsa:MessageID>
<wsa:RelatesTo>urn:uuid:704ccde5-6861-415d-bd65-31dd9d7a8b98</wsa:RelatesTo>
</SOAP-ENV:Header>
<SOAP-ENV:Body>
<mex:Metadata>
<mex:MetadataSection Dialect="http://schemas.xmlsoap.org/ws/2006/02/devprof/ThisDevice">
<wsdp:ThisDevice>
<wsdp:FriendlyName xml:lang="en">Printer (HP Color LaserJet CM1312nfi MFP)</wsdp:FriendlyName>
<wsdp:FirmwareVersion>20140625</wsdp:FirmwareVersion>
<wsdp:SerialNumber>CNB885H665</wsdp:SerialNumber>
</wsdp:ThisDevice>
</mex:MetadataSection>
<mex:MetadataSection Dialect="http://schemas.xmlsoap.org/ws/2006/02/devprof/ThisModel">
<wsdp:ThisModel>
<wsdp:Manufacturer xml:lang="en">HP</wsdp:Manufacturer>
<wsdp:ManufacturerUrl>http://www.hp.com/</wsdp:ManufacturerUrl>
<wsdp:ModelName xml:lang="en">HP Color LaserJet CM1312nfi MFP</wsdp:ModelName>
<wsdp:ModelNumber>CM1312nfi MFP</wsdp:ModelNumber>
<wsdp:PresentationUrl>http://192.168.1.20:80/</wsdp:PresentationUrl>
<PNPX:DeviceCategory>Printers</PNPX:DeviceCategory>
</wsdp:ThisModel>
</mex:MetadataSection>
<mex:MetadataSection Dialect="http://schemas.xmlsoap.org/ws/2006/02/devprof/Relationship">
<wsdp:Relationship Type="http://schemas.xmlsoap.org/ws/2006/02/devprof/host">
<wsdp:Hosted>
<wsa:EndpointReference>
<wsa:Address>http://192.168.1.20:3910/</wsa:Address>
<wsa:ReferenceProperties>
<UNS1:ServiceIdentifier>uri:prn</UNS1:ServiceIdentifier>
</wsa:ReferenceProperties>
</wsa:EndpointReference>
<wsdp:Types>wprt:PrinterServiceType</wsdp:Types>
<wsdp:ServiceId>uri:1cd4F16e-7c8a-a7a0-3797-00145a8827ce</wsdp:ServiceId>
<PNPX:CompatibleId>http://schemas.microsoft.com/windows/2006/08/wdp/print/PrinterServiceType</PNPX:CompatibleId>
</wsdp:Hosted>
</wsdp:Relationship>
</mex:MetadataSection>
</mex:Metadata>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
EOM;
$xml = simplexml_load_string($str);
$namespaces = $xml->getNamespaces(true);
// Here we are saying that we want the Body node in the SOAP-ENV namespace
$body = $xml->children( $namespaces['SOAP-ENV'] )->Body;
// Inside that Body node, we want to get into the mex namespace
$mex = $body->children( $namespaces['mex'] );
// We want the MetadataSections that are in each of the mex namespaces
$metadataSections = $mex->Metadata->MetadataSection;
// Loop through each of the MetadataSections
foreach( $metadataSections as $meta )
{
// Get inside the wsdp namespace
$wsdp = $meta->children( $namespaces['wsdp'] );
// Check if there is a Hosted node inside a Relationship node
if( isset( $wsdp->Relationship->Hosted ) )
{
// Get the wsa namespace inside the Hosted node
$wsa = $wsdp->Relationship->Hosted->children( $namespaces['wsa'] );
// If there is an Address inside the EndpointReference node
if( isset( $wsa->EndpointReference->Address ) )
{
// Then output it
echo $wsa->EndpointReference->Address;
}
}
}
As an extremely simple example - if you just wanted the wsa:Address element...
$myxml1 = simplexml_load_string($test_1);
$myxml1->registerXPathNamespace("wsa", "http://schemas.xmlsoap.org/ws/2004/08/addressing");
echo "wsa:Address=".(string)$myxml1->xpath("//wsa:Address")[0];
This just makes sure that the wsa namespace is registered with the document and available to XPath expressions. Then the XPath expression just says - fetch element wsa:Address from anywhere in the document. But as xpath returns a list of all matches (even if there is only 1) so use [0] to get the first item. This outputs...
wsa:Address=http://192.168.1.20:3910/
If you needed more data around the (for example) <wsdp:Hosted> element, you could do something like...
$myxml1 = simplexml_load_string($test_1);
$myxml1->registerXPathNamespace("wsdp", "http://schemas.xmlsoap.org/ws/2006/02/devprof");
$hosted = $myxml1->xpath("//wsdp:Hosted")[0];
$hostedWSA = $hosted->children("wsa", true);
echo "wsa:Address=".(string)$hostedWSA->EndpointReference->Address.PHP_EOL;
$hostedWSPD = $hosted->children("wsdp", true);
echo "wsdp:Types=".(string)$hostedWSPD->Types.PHP_EOL;
So instead this starts of by fetching the correct element and then working with the various child nodes in the different namespaces within that node.
Related
I'm creating an xml file with PHP.
The file I need to create is this one I show you:
<p:FatturaElettronica versione="FPA12" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:p="http://microsoft.com/wsdl/types/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" versione="FPA12" >
<FatturaElettronicaHeader>
<DatiTrasmissione>
<IdTrasmittente>
<IdPaese>IT</IdPaese>
<IdCodice>01234567890</IdCodice>
</IdTrasmittente>
<ProgressivoInvio>00001</ProgressivoInvio>
<FormatoTrasmissione>FPA12</FormatoTrasmissione>
<CodiceDestinatario>AAAAAA</CodiceDestinatario>
</DatiTrasmissione>
</FatturaElettronicaHeader>
<p:FatturaElettronica>
This is my code:
$xml = new SimpleXMLElement('<p:FatturazioneElettronica xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:p="http://microsoft.com/wsdl/types/" />');
$xml->addAttribute("versione","FPA12");
$xml->addAttribute("xmlns:xmlns:xsi","http://www.w3.org/2001/XMLSchema-instance");
$FatturaElettronicaHeader = $xml->addChild('FatturaElettronicaHeader');
$DatiTrasmissione=$FatturaElettronicaHeader->addChild('DatiTrasmissione');
$IdTrasmittente=$DatiTrasmissione->addChild('IdTrasmittente');
$IdTrasmittente->addChild('IdPaese', 'IT');
$IdTrasmittente->addChild('IdCodice','01234567890');
$ProgressivoInvio=$DatiTrasmissione->addChild('ProgressivoInvio', '00001');
$FormatoTrasmissione=$DatiTrasmissione->addChild('DatiTrasmissione', 'FPA12');
$CodiceDestinatario=$DatiTrasmissione->addChild('CodiceDestinatario', 'AAAAAA');
Because in my created file I initially had the prefix p: in each tag, as shown below
<p:FatturazioneElettronica xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:p="http://microsoft.com/wsdl/types/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" versione="FPA12">
<p:FatturaElettronicaHeader>
<p:DatiTrasmissione>
<p:IdTrasmittente>
<p:IdPaese>IT</p:IdPaese>
<p:IdCodice>01234567890</p:IdCodice>
</p:IdTrasmittente>
<p:ProgressivoInvio>00001</p:ProgressivoInvio>
<p:DatiTrasmissione>FPA12</p:DatiTrasmissione>
<p:CodiceDestinatario>AAAAAA</p:CodiceDestinatario>
</p:DatiTrasmissione>
while this prefix p: must be only in the root node (p:FatturaElettronica) I added xmlns="http://dummy.com"
<p:FatturazioneElettronica xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:p="http://microsoft.com/wsdl/types/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" versione="FPA12" xmlns="http://dummy.com">
and
$fatturaelettronicaheader = $xml->addChild('FatturaElettronicaHeader', '', 'http://dummy.com');
as it was suggested in this question
Only this 'http://dummy.com' is not present in the original xml file.
How can I solve this problem or possibly eliminate it before actually generating the file?
SimpleXML abstracts nodes and has some automatic logic for namespaces. That works fine for basic/simple XML structures.
For more complex XML structures you want to be explicit - so use DOM. It has specific methods for different node types with and without namespaces.
// define a list with the used namespaces
$namespaces = [
'xmlns' => 'http://www.w3.org/2000/xmlns/',
'xsi' => 'http://www.w3.org/2001/XMLSchema-instance',
'signature' => 'http://www.w3.org/2000/09/xmldsig#',
'wsdl-types' => 'http://microsoft.com/wsdl/types/'
];
$document = new DOMDocument('1.0', 'UTF-8');
// create and append an element with a namespace
// this will add the namespace definition for the prefix "p" also
$document->appendChild(
$root = $document->createElementNS($namespaces['wsdl-types'], 'p:FatturazioneElettronica')
);
// set an attribute without a namespace
$root->setAttribute('versione', 'FPA12');
// add namespace definitions using the reserved "xmlns" namespace
$root->setAttributeNS($namespaces['xmlns'], 'xmlns:xsi', $namespaces['xsi']);
$root->setAttributeNS($namespaces['xmlns'], 'xmlns:ds', $namespaces['signature']);
// create and append the an element - keep in variable for manipulation
// the element does not have a namespace
$root->appendChild(
$header = $document->createElement('FatturaElettronicaHeader')
);
$header->appendChild(
$dati = $document->createElement('DatiTrasmissione')
);
$dati->appendChild(
$id = $document->createElement('IdTrasmittente')
);
// create and append element, set text content using a chained call
$id
->appendChild($document->createElement('IdPaese'))
->textContent = 'IT';
$id
->appendChild($document->createElement('IdCodice'))
->textContent = '01234567890';
$dati
->appendChild($document->createElement('ProgressivoInvio'))
->textContent = '00001';
$dati
->appendChild($document->createElement('FormatoTrasmissione'))
->textContent = 'FPA12';
$dati
->appendChild($document->createElement('CodiceDestinatario'))
->textContent = 'AAAAAA';
$document->formatOutput = TRUE;
echo $document->saveXML();
Output:
<?xml version="1.0" encoding="UTF-8"?>
<p:FatturazioneElettronica xmlns:p="http://microsoft.com/wsdl/types/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" versione="FPA12">
<FatturaElettronicaHeader>
<DatiTrasmissione>
<IdTrasmittente>
<IdPaese>IT</IdPaese>
<IdCodice>01234567890</IdCodice>
</IdTrasmittente>
<ProgressivoInvio>00001</ProgressivoInvio>
<FormatoTrasmissione>FPA12</FormatoTrasmissione>
<CodiceDestinatario>AAAAAA</CodiceDestinatario>
</DatiTrasmissione>
</FatturaElettronicaHeader>
</p:FatturazioneElettronica>
Be aware that in your XML p:FatturazioneElettronica has a namespace. It resolves to {http://microsoft.com/wsdl/types/}FatturazioneElettronica. However I don't think that FatturazioneElettronica is a valid element in the WSDL types namespace.
FatturaElettronicaHeader (and the descandant nodes) do not have a namespace.
First, your desired xml (as well as the one in the question you link to) is not well formed for several reasons.
Second, even after that's fixed (see below), it's not clear to me why you're going about it the way you do.
How about this way:
$string = '<?xml version="1.0" encoding="UTF-8"?>
<root>
<p:FatturaElettronica xmlns:p="http://microsoft.com/wsdl/types/" xmlns:ds="http://www.w3.org/2000/09/xmldsig#" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" versione="FPA12">
<FatturaElettronicaHeader>
<DatiTrasmissione>
<IdTrasmittente>
<IdPaese>IT</IdPaese>
<IdCodice>01234567890</IdCodice>
</IdTrasmittente>
<ProgressivoInvio>00001</ProgressivoInvio>
<FormatoTrasmissione>FPA12</FormatoTrasmissione>
<CodiceDestinatario>AAAAAA</CodiceDestinatario>
</DatiTrasmissione>
</FatturaElettronicaHeader>
</p:FatturaElettronica>
</root>';
$xml = simplexml_load_string($string);
echo $xml->asXML() ."\r\n";
That should echo your well-formed xml.
This is what the output should be:
<invoice:company this="1">
<invoice:transport from="7777777777" to="77777777777">
<invoice:via via="7777777777" id="1"/>
</invoice:transport>
</invoice:company>
But I am getting this:
<company this="1">
<transport from="7777777777" to="77777777777">
<via via="7777777777" id="1"/>
</transport>
</company>
I am using this as XML generator:
$xml = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?><invoice>
</invoice>');
//child of invoice
$company= $xml->addChild('company');
//child of company
$transport = $processing->addChild('transport');
$transport->addAttribute('to','77777777777');
$transport->addAttribute('from','77777777777');
//child of transport
$via = $transport->addChild('via');
$via->addAttribute('id','1');
$via->addAttribute('via','77777777777');
$xml->saveXML();
$xml->asXML("company_001.xml");'
Why is ":" on the element tag? how can I do that? I need to have that too.
As mentioned in the comment, invoice: is the namespace of the elements in the document.
When creating an XML document with a namespace, you need to declare it. In the code below, in this I've done it in the initial document loaded into SimpleXMLElement. I don't know the correct definition of this namespace - so I've used "http://some.url" throughout (and all references need to be changed). If you don't define this namespace, SimpleXML will add it's own definition the first time you use it.
When adding the elements in, you can define which namespace they get added to, the third parameter of addChild is the namespace.
So...
$xml = new SimpleXMLElement('<?xml version="1.0" encoding="utf-8"?>
<invoice xmlns:invoice="http://some.url">
</invoice>');
//child of invoice
$processing= $xml->addChild('company', "", "http://some.url");
//child of company
$transport = $processing->addChild('transport', "", "http://some.url");
$transport->addAttribute('to','77777777777');
$transport->addAttribute('from','77777777777');
//child of transport
$via = $transport->addChild('via', "", "http://some.url");
$via->addAttribute('id','1');
$via->addAttribute('via','77777777777');
echo $xml->asXML();
Produces (I've formated the output to help)...
<?xml version="1.0" encoding="utf-8"?>
<invoice xmlns:invoice="http://some.url">
<invoice:company>
<invoice:transport to="77777777777" from="77777777777">
<invoice:via id="1" via="77777777777" />
</invoice:transport>
</invoice:company>
</invoice>
As I'm not sure if this is the entire document your creating, there may need to be minor changes, but hope this helps.
How do I get the value of "TagOne" (i.e. foo) and TagTwo (i.e. bar) from the XML below using
simplexml_load_string? I'm stumped by the namespace called "ns" in the tag.
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Body>
<ns:ExampleInterface_Output xmlns:ns="http://example.com/interfaces">
<ns:TagOne>Foo</ns:TagOne>
<ns:TagTwo>Bar</ns:TagTwo>
</ns:ExampleInterface_Output>
</SOAP-ENV:Body>
Thanks very much for your kind help!
Check this code:
<?php
$xml = <<<XML
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<SOAP-ENV:Body>
<ns:ExampleInterface_Output xmlns:ns="http://example.com/interfaces">
<ns:TagOne>Foo</ns:TagOne>
<ns:TagTwo>Bar</ns:TagTwo>
</ns:ExampleInterface_Output>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
XML;
$xse = new SimpleXMLElement($xml);
$exampleInterface = $xse
->children('SOAP-ENV', true)
->children('ns', true);
foreach ($exampleInterface->children('ns', true) as $key => $value) {
//Do your stuff
}
Well you can declare the "ns" namespace to simplexml_load_string like this:
$xml = simplexml_load_string($string, "SimpleXMLElement", 0, "ns", TRUE);
This says that "ns" is a namespace prefix (rather than a namespace URL). See the PHP doc page for simplexml_load_string for more details.
Another problem is that the Body element has a "SOAP-ENV" prefix which is not declared anywhere in the XML, so you will get a warning about this. However, the value of $xml will become an object structured like this:
SimpleXMLElement Object (
[ExampleInterface_Output] => SimpleXMLElement Object (
[TagOne] => Foo
[TagTwo] => Bar
)
)
But, warning aside, this might be exactly what you need. If the warning is a problem, you can simply remove the "SOAP-ENV" prefix from the start and end tags of the Body element.
I am having a small problem parsing the tags in the nested namespace called "q0:", located below.
//Tag that I am trying to parse
<q0:CustomerTransactionId>
I am able to get to all of the "v12:" namespaced tags but not the "q0:" ones for some reason.
Thanks!
Here is the XML
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<env:Header
xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"/>
<soapenv:Body>
<v12:ProcessShipmentReply
xmlns:env="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:v12="http://fedex.com/ws/ship/v12">
<v12:HighestSeverity>ERROR</v12:HighestSeverity>
<v12:Notifications>
<v12:Severity>ERROR</v12:Severity>
<v12:Source>ship</v12:Source>
<v12:Code>3058</v12:Code>
<v12:Message>Recipient Postal code or routing code is required</v12:Message>
<v12:LocalizedMessage>Recipient Postal code or routing code is required</v12:LocalizedMessage>
</v12:Notifications>
<q0:TransactionDetail xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:q0="http://fedex.com/ws/ship/v12">
<q0:CustomerTransactionId>21445634</q0:CustomerTransactionId>
</q0:TransactionDetail><q0:Version xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:q0="http://fedex.com/ws/ship/v12">
<q0:ServiceId>ship</q0:ServiceId>
<q0:Major>12</q0:Major>
<q0:Intermediate>0</q0:Intermediate>
<q0:Minor>0</q0:Minor>
</q0:Version>
</v12:ProcessShipmentReply>
</soapenv:Body>
</soapenv:Envelope>
And here is my parse
$xml = simplexml_load_string($result, NULL, NULL, 'http://schemas.xmlsoap.org/soap/envelope/');
$xml->registerXPathNamespace('env', 'http://schemas.xmlsoap.org/soap/envelope/');
$xml->registerXPathNamespace('v12', 'http://fedex.com/ws/ship/v12');
$xml->registerXPathNamespace('q0', 'http://fedex.com/ws/ship/v12');
$bodies = $xml->xpath('env:Body');
foreach($bodies as $body){
$reply = $body->children('v12', TRUE)->ProcessShipmentReply;
$reply2 = $body->children('q0', TRUE)->TransactionDetail;
$custInfoArr['status'] = (string) $reply->HighestSeverity;
if(strtolower($custInfoArr['status'])=="error"){
$custInfoArr['invoiceNum'] = (string)$reply2->CustomerTransactionId;
$custInfoArr['notificationSeverity']= (string) $reply->Notifications->Severity;
$custInfoArr['notificationSource']= (string) $reply->Notifications->Source;
$custInfoArr['notificationCode']= (string) $reply->Notifications->Code;
$custInfoArr['notificationMessage']= (string) $reply->Notifications->Message;
$custInfoArr['notificationLocalizedMessage']= (string) $reply->Notifications->LocalizedMessage;
}
$custInfoArr['trackingCode'] = (string) $reply->CompletedShipmentDetail->CompletedPackageDetails->TrackingIds->TrackingNumber;
$custInfoArr['labelCode'] = (string) $reply->CompletedShipmentDetail->CompletedPackageDetails->Label->Parts->Image;
$custInfoArr['labelCode'] = base64_decode($custInfoArr['labelCode']);
}
<q0:TransactionDetail> is not a child of <env:Body>, it is a child of <v12:ProcessShipmentReply>, so you need to look for it inside $reply, not $body: $reply2 = $reply->children('q0', TRUE)->TransactionDetail;
The important thing to remember is that both the ->TagName operator and the ->children() method only look at immediate children of a particular node, they are not doing a "deep search" like XPath.
In fact, looking, both v12: and q0: are aliases for the same namespace ('http://fedex.com/ws/ship/v12'), so the next line can just be $reply2 = $reply->TransactionDetail. If you just say $reply = $body->children('http://fedex.com/ws/ship/v12')->ProcessShipmentReply rather than relying on the alias, this becomes clearer (and safer, since those aliases may change).
Incidentally, unless you're using it for something else, you can also get rid of all your XPath code (including all of the registerXPathNamespace calls) and just write $body = $xml->children('http://schemas.xmlsoap.org/soap/envelope/')->Body (I'm pretty sure SOAP only allows one Body per message).
I'm able to parse an XML SOAP when I know Namespace and Request name.
Because I have different kind of SOAP requests, I would like to get the Request name in the SOAP file . Extract of a part of my SOAP:
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope
xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns1="http://schema.example.com"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/"
SOAP-ENV:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/"
>
<SOAP-ENV:Body>
**<ns1:SendMailling>**
<campagne xsi:type="ns1:Campaign"><ActivateDedup xsi:nil="true"/><BillingCode xsi:nil="true"/><DeliveryFax xsi:type="ns1:DeliveryFax"/>
<DeliveryMail xsi:type="ns1:DeliveryMail">
...
PHP Code:
if(is_file($file))
{
$content=file_get_contents($file);
$xml = simplexml_load_string($content);
$xml->registerXPathNamespace('ns1', 'http://schema.example.com');
foreach ($xml->xpath('\\SOAP-ENV:') as $item)
{
//certainly the bad way?
echo "<pre>";
print_r($item);
echo "</pre>";
}
echo "<pre>";
print_r($xml);
echo "</pre>";
}
I get no result... I would like to make appears : 'SendMailling' (identify the request name)
When I specifically specify
//foreach($xml->xpath('//ns1:SendMailling') as $item)
there is no problems.
I tried foreach($xml->xpath('//ns1') as $item)
and $xml->xpath('//SOAP-ENC'), $xml->xpath('//Body') but...
I had problems to understand your questions so this probably is not the answer.
If I understand you right, you want to select all Element Nodes that are a direct children of <SOAP-ENV:Body> and that are in the ns1 / http://schema.example.com namespace.
You already have registered the namespace prefix to be used with SimpleXMLElement::xpath:
$xml->registerXPathNamespace('ns1', 'http://schema.example.com');
You have not yet registered the SOAP-ENV / http://schemas.xmlsoap.org/soap/envelope/ namespace as far as I can see.
In XPath to match an element you can specify it's namespace. There are multiple ways how this is done:
* All elements in any namespace.
prefix:* All elements in namespace "prefix" (registered prefix)
prefix:local Only "local" elements in namespace "prefix"
For example to select all elements with the ns1 prefix:
//ns1:*
You might want to limit this because you only want direct children withn <SOAP-ENV:Body> so register that namespace with the SOAP-ENV prefix and extend the previous xpath:
/SOAP-ENV:Body/ns1:*
This should then have all those elements you're looking for.
(OP:) Thanks again, when I do a
foreach ($xml->xpath('//SOAP-ENV:Body/ns1:*') as $item) {
echo $item->getName() . "<br>";
}
All is ok I get the request Name 'SendMailling'.