Scrappy Cocco
Scrappy Cocco

Reputation: 1204

php parse the string obtained inside the XML tag

Recently i had to do an api call which returned an XML response. But it seems like a strange XML with only 1 base tag and all other info is given as string inside this base tag. I need to get the value of each of these string tags, But don't know how to achieve this.

Below given is the sample response am receiving from the api.

<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://webservices/api/v1/Inventory.asmx">
&lt;NewDataSet&gt;
    &lt;Table&gt;
        &lt;ITEMNO&gt;120&lt;/ITEMNO&gt;
        &lt;IDESC&gt;BERG 22423 BULL .224 90GR VLD TGT  100&lt;/IDESC&gt;
        &lt;IMFSEQ&gt;570&lt;/IMFSEQ&gt;
        &lt;IMFGNO&gt;628&lt;/IMFGNO&gt;
        &lt;CSEQ&gt;0&lt;/CSEQ&gt;
        &lt;ITYPE&gt;7&lt;/ITYPE&gt;
        &lt;SHDESC&gt;Berger Bullets 22423 Target 22 Caliber .224 90 GR Target Very Low Drag 100 Box                                                                                                                          &lt;/SHDESC&gt;
        &lt;UOM&gt;EA&lt;/UOM&gt;
        &lt;PRC1&gt;34.73&lt;/PRC1&gt;
        &lt;CPRC&gt;34.04&lt;/CPRC&gt;
        &lt;QTYOH&gt;26&lt;/QTYOH&gt;
        &lt;WTPBX&gt;1.400&lt;/WTPBX&gt;
        &lt;ITUPC&gt;679459224239   &lt;/ITUPC&gt;
        &lt;MFGINO&gt;22423       &lt;/MFGINO&gt;
        &lt;SCNAM1&gt;BERG   &lt;/SCNAM1&gt;
        &lt;SCNAM2&gt;B22CAL &lt;/SCNAM2&gt;
        &lt;CATCD xml:space="preserve"&gt; &lt;/CATCD&gt;
        &lt;MFPRTYP xml:space="preserve"&gt; &lt;/MFPRTYP&gt;
        &lt;MFPRC&gt;0.00&lt;/MFPRC&gt;
        &lt;CATID&gt;36&lt;/CATID&gt;
        &lt;TXTREF&gt;120&lt;/TXTREF&gt;
        &lt;PICREF&gt;120&lt;/PICREF&gt;
        &lt;ITBRDNO&gt;628&lt;/ITBRDNO&gt;
        &lt;SERIES&gt;Match Grade                                                                                                                                                                                             &lt;/SERIES&gt;
        &lt;LENGTH&gt;4.1&lt;/LENGTH&gt;
        &lt;HEIGHT&gt;1.2&lt;/HEIGHT&gt;
        &lt;WIDTH&gt;3.3&lt;/WIDTH&gt;
        &lt;HAZAIR xml:space="preserve"&gt; &lt;/HAZAIR&gt;
        &lt;HAZGRND xml:space="preserve"&gt; &lt;/HAZGRND&gt;
        &lt;CHGDTE&gt;2018-05-23T00:00:00+00:00&lt;/CHGDTE&gt;
        &lt;LOADDT&gt;2015-11-05T00:00:00+00:00&lt;/LOADDT&gt;
    &lt;/Table&gt;
&lt;/NewDataSet&gt;
</string>

I tried the below methods but it does not seems to work as needed

<?php
$xml=simplexml_load_string($myXMLData);
print_r($xml);

$xml = new SimpleXMLElement($myXMLData);
print_r($xml);
?>

what am looking is to get the value of each of the tags inside the response. Any help is much appreciated.

Upvotes: 1

Views: 201

Answers (2)

ThW
ThW

Reputation: 19512

You have an XML document as text inside another XML document. So you have to load the outer document, fetch the text content and parse it as XML again:

$xml = <<<'XML'
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://webservices/api/v1/Inventory.asmx">
&lt;NewDataSet&gt;
    &lt;Table&gt;
        &lt;ITEMNO&gt;120&lt;/ITEMNO&gt;
        &lt;IDESC&gt;BERG 22423 BULL .224 90GR VLD TGT  100&lt;/IDESC&gt;
    &lt;/Table&gt;
&lt;/NewDataSet&gt;
</string>
XML;

$stringElement = new SimpleXMLELement($xml);
echo $stringElement, "\n";

// force to string and parse as XML
$newDataSet = new SimpleXMLElement((string)$stringElement);
echo $newDataSet->Table->ITEMNO;

Output:

<NewDataSet>
  <Table>
    <ITEMNO>120</ITEMNO> 
    <IDESC>BERG 22423 BULL .224 90GR VLD TGT 100</IDESC>
  </Table> 
</NewDataSet> 

120

Upvotes: 1

Jeto
Jeto

Reputation: 14927

You could make use of html_entity_decode to transform your string into proper XML:

$xml = new SimpleXMLElement(html_entity_decode($myXMLData));
print_r($xml);

Demo here: https://3v4l.org/AWWEH

Upvotes: 1

Related Questions