Get Value of an XML (XSD) with PHP

0

I connect to a SOAP service that returns the following:

<xs:schema xmlns:mstns="http://tempuri.org/dsPDFGuia.xsd" xmlns="http://tempuri.org/dsPDFGuia.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" id="dsPDFGuia" targetnamespace="http://tempuri.org/dsPDFGuia.xsd" attributeformdefault="qualified" elementformdefault="qualified"><xs:element name="dsPDFGuia" msdata:isdataset="true" msdata:usecurrentlocale="true"><xs:complextype><xs:choice minoccurs="0" maxoccurs="unbounded"><xs:element name="Guia"><xs:complextype><xs:sequence><xs:element name="Inicial" type="xs:long" minoccurs="0"><xs:element name="Final" type="xs:long" minoccurs="0"><xs:element name="BytesImagen" type="xs:base64Binary" minoccurs="0"></xs:element></xs:element></xs:element></xs:sequence></xs:complextype></xs:element></xs:choice></xs:complextype></xs:element></xs:schema><diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1"><dspdfguia xmlns="http://tempuri.org/dsPDFGuia.xsd"><guia diffgr:id="Guia1" msdata:roworder="0"><inicial>290005680</inicial><final>290005680</final><bytesimagen>ARCHIVO_CODIFICADO_BASE64</bytesimagen></guia></dspdfguia></diffgr:diffgram>

For what I need to use the content of the tag bytesimage but I do not know how to get this value from PHP. Getting this value I know how to decode it and use it to show the pdf, but it gets in the way of all the xml code.

    
asked by Manuel Fernandez 12.04.2018 в 17:38
source

2 answers

0

After searching and searching I did not get a good method. However, use my common sense and using str_replace from php remove all the code that was in my way ( XSD ), leaving only the binary file to decode and show. Then I leave the code that gave solution so that it can be consulted by another person!

$pdf = str_replace('<xs:schema xmlns:mstns="http://tempuri.org/dsPDFGuia.xsd" xmlns="http://tempuri.org/dsPDFGuia.xsd" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" id="dsPDFGuia" targetNamespace="http://tempuri.org/dsPDFGuia.xsd" attributeFormDefault="qualified" elementFormDefault="qualified"><xs:element name="dsPDFGuia" msdata:IsDataSet="true" msdata:UseCurrentLocale="true"><xs:complexType><xs:choice minOccurs="0" maxOccurs="unbounded"><xs:element name="Guia"><xs:complexType><xs:sequence><xs:element name="Inicial" type="xs:long" minOccurs="0"/><xs:element name="Final" type="xs:long" minOccurs="0"/><xs:element name="BytesImagen" type="xs:base64Binary" minOccurs="0"/></xs:sequence></xs:complexType></xs:element></xs:choice></xs:complexType></xs:element></xs:schema><diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1"><dsPDFGuia xmlns="http://tempuri.org/dsPDFGuia.xsd"><Guia diffgr:id="Guia1" msdata:rowOrder="0"><Inicial></Inicial><Final></Final><BytesImagen>',"",$pdf);
$pdf = str_replace('</BytesImagen></Guia></dsPDFGuia></diffgr:diffgram>',"",$pdf);

header('Content-type: application/pdf');
echo base64_decode($pdf);
    
answered by 28.04.2018 / 00:49
source
-1

Without having information about your code or the service, I can think of two possible solutions, the first is using regular expressions and preg_match () :

$xml = /* Aquí va tu XML */;

$xmlData = preg_match("/(?s)(?<=\<bytesimagen\>)(.*?)(?=\<\/bytesimagen\>)/", $xml, $matchesXML);

$b64 = array_pop($matchesXML); // Este es el valor que necesitas

The next option is to use simplexml_load_string () and it's more correct but you should correct the XML that you expose since it returns error, indicating that there is extra content that should not be there.

$xml = /* Aquí va tu XML */;

$xmlData = json_decode(json_encode(simplexml_load_string($xml)), 1);

$b64 = $xmlData['dspdfguia']['guia']['bytesimagen']; // Este es el valor que necesitas
    
answered by 14.04.2018 в 10:26