How to parse XML in PHP

Hi guys i am trying to use phpse xml file in php using simplexmlelement.

 <OrderResponse>
      <OrderResponseReferences>
           <BuyersOrderNumber Preserve="true">100000002</BuyersOrderNumber>
           <SuppliersOrderReference Preserve="true">6711637</SuppliersOrderReference>
      </OrderResponseReferences>
 </OrderResponse>

      

I am using the following code to access BuyersOrderNumber

$local_file_path = "C:/etrade_files/Orders/order_acknowledgements/ORA1707220-05-2015--16-48-31.xml";

$xml = simplexml_load_file($local_file_path);

foreach($xml->children() as $child) 
{
    $child = $child->getName();

    if($child == "OrderResponseReferences")
    {
        $order_reference = $xml->$child->BuyersOrderNumber;

        print_r($order_reference);
    }   
}

      

Result:

 SimpleXMLElement Object ( [@attributes] => Array ( [Preserve] => true ) [0] => 100000002 )

      

How can I get the order number 100000002

Also, these files have multiple order lines, it would really help me if anyone can explain the logic that I can use to get all the information I need.

 <OrderResponseLine>
      <LineNumber>1</LineNumber>
      <OrderResponseLineReferences>
           <BuyersOrderLineReference Preserve="true">100000002</BuyersOrderLineReference>
      </OrderResponseLineReferences>
      <Product>
           <SuppliersProductCode>108696</SuppliersProductCode>
      </Product>
      <Quantity>
           <Amount>2</Amount>
      </Quantity>
      <LineTotal>331.18</LineTotal>
 </OrderResponseLine>

      

This is my first time working with xml, I would really appreciate if someone could help. Thank.

+3


source to share


4 answers


If you add an object to a string, it will use its value and convert that:

print_r((string) $order_reference);
// 100000002

      



I tried your code and you are good! It just (string)

should be enough to get the script running.

0


source


I would recommend that you use the DOM to parse this XML.

$xml = new DOMDocument();
$xml->load('ORA1707220-05-2015--16-48-31.xml');
$root = $xml->getElementsByTagName('OrderResponse');
$tree = $root->item(0);
$child = $tree->childNodes;
foreach($child as $children){
    switch($children->nodeName){
         case 'OrderResponseReferences':
             //instructions
    }
}

      



And use nodeValue

node to get the value.

0


source


The DOMDocument function and recursive seems useful to me to get this value.

$xml = new DOMDocument();
$xml->load('ORA1707220-05-2015--16-48-31.xml');
$root = $xml->getElementsByTagName('OrderResponse');
$tree = $root->item(0);

parse($tree);

function parse($tree){
    $child = $tree->childNodes;

    foreach($child as $children){
        switch($children->nodeName){
             //Case
                 //instructions
        }


        if($child->hasChildNodes())
            parse($child);
        }

 }

      

If there are trays in the current node, it will call parse () again on the child and you will get the yu want value

0


source


There are much simpler ways :

extract many <BuyersOrderNumber>

:

use xpath

:

$numbers = $xml->xpath("//BuyersOrderNumber");

      

Explanation:

  • operator xpath

    will choose any<BuyersOrderNumber>

  • anywhere in the XML tree (due to the //

    pre-named node)
  • $numbers

    will contain an array of elements SimpleXML

List all nodes:

foreach ($numbers as $n)
    echo $n; 

      

select one <BuyersOrderNumber>

node

  • standard way: if there is only one such node in XML:

    echo $xml->OrderResponseReferences->BuyersOrderNumber;
    
          

  • again xpath

    :

    echo $xml->xpath("//BuyersOrderNumber")[0];
    
          

    get all nodes and only display the first one (what is why [0]

    )

see working example: https://eval.in/368739

Of course, $xml

there are examples.

$xml = simplexml_load_file($local_file_path);

      

Link: http://php.net/manual/en/simplexml.examples-basic.php

0


source







All Articles