Home > Enterprise >  PHP XML Extract data out of the tag or field
PHP XML Extract data out of the tag or field

Time:07-08

I know how to extract the data from between two tags or elements but I do not know how to extract the data from within the element itself for example (see this section Tax="0.0" EHF="0.0" Freight="0.0" Handling="0.0" SubTotal="0.72"). Any help would be most appreciated.

<INVTOTAL Tax="0.0" EHF="0.0" Freight="0.0" Handling="0.0" SubTotal="0.72">0.72</INVTOTAL>

CodePudding user response:

There are a few ways to do it. For example, using xpath, something like

$invoice= '
<root><INVTOTAL Tax="10.0" EHF="0.0" Freight="0.0" Handling="0.0" SubTotal="0.72">0.72</INVTOTAL></root>';
$XMLDoc = new DOMDocument();
$XMLDoc->loadXML($invoice);
$xpath = new DOMXPath($XMLDoc);

$tax = $xpath->query("//INVTOTAL/@Tax");
echo($tax[0]->nodeValue);

Output:

10.0
  • Related