1

Hi all I have an XML file like this:

<?xml version="1.0"?>
<catalog>
   <car>
      <title>This is car</title>
      <price>44.95</price>
      <model>2018</model>
      <description>An in-depth look at creating applications with XML.</description>
   </car>

   <bike>
      <title>this is bike</title>
      <price>33.58</price>
      <description>Just the dummy description</description>
   </bike>
   <wheels>
      <title>this is wheel</title>
      <price>33.58</price>
      <description>Just the dummy description</description>
   </wheels>

   <bike>
      <title>this is bike</title>
      <price>33.58</price>
      <description>Just the dummy description</description>
   </bike>


</catalog>

I want to loop through the nodes and process the nodes accordingly. If it's a car, I need to pass the car details to some function and if it's a bike then I need to pass the bike details to the other function.

The point is to process the nodes based on the node type (car or bike or other).

PHP

$z = new XMLReader;
$z->open('data.xml');

$doc = new DOMDocument;

// move to the first <product /> node
while ($z->read() && $z->name !== 'product');

// now that we're at the right depth, hop to the next <product/> until the end of the tree
while ($z->name === 'product')
{
    // either one should work
    //$node = new SimpleXMLElement($z->readOuterXML());
    $node = simplexml_import_dom($doc->importNode($z->expand(), true));

    // now you can use $node without going insane about parsing
    var_dump($node->element_1);

    // go to next <product />
    $z->next('product');
}

The above PHP code works fine if the nodes are products. I need to work with different nodes.

5
  • My question might sound stupid, but are you changing your hard-coded 'product' strings to whatever else you're trying to find? Commented May 21, 2019 at 12:36
  • we have to change that, but how to know which is next node? Commented May 21, 2019 at 12:42
  • Shouldn't the XmlReader take care of that? If you have $z->next('bike') it should give you the next <bike>, right? Commented May 21, 2019 at 13:28
  • @MarkusDeibel yes, but problem is the next element is not always bike only it can be anything. i mean the order is not fixed. we need to check manually. Commented May 21, 2019 at 14:08
  • Well, if the notes on the documentation page (php.net/manual/de/xmlreader.next.php#123774) can be trusted "next() without parameters will bring you to the next sibling node on the same depth as the depth where the current cursor is.". The only thing would be to get to the first child with while ($z->read() && $z->name === 'catalog'); However, after looking at the manual I think XMLReader is rather basic and another lib will give you a better API. Commented May 22, 2019 at 6:38

1 Answer 1

1

You can do this just using SimpleXML:

$xml = simplexml_load_file('data.xml');
foreach ($xml->children() as $product) {
    if ($product->getName() == 'car') {
        carFunc($product->title, $product->price, $product->model, $product->description);
    }
    if ($product->getName() == 'bike') {
        bikeFunc($product->title, $product->price, $product->description);
    }
    if ($product->getName() == 'wheels') {
        wheelFunc($product->title, $product->price, $product->description);
    }
}

function carFunc($title, $price, $model, $description) {
    echo "Car: $title: It's a $model selling for $price. In detail: $description\n";
}

function bikeFunc($title, $price, $description) {
    echo "Bike: $title: It sells for $price. In detail: $description\n";
}

function wheelFunc($title, $price, $description) {
    echo "Wheel: $title: It sells for $price. In detail: $description\n";
}

Output (for your sample XML)

Car: This is car: It's a 2018 selling for 44.95. In detail: An in-depth look at creating applications with XML. 
Bike: this is bike: It sells for 33.58. In detail: Just the dummy description 
Wheel: this is wheel: It sells for 33.58. In detail: Just the dummy description 
Bike: this is bike: It sells for 33.58. In detail: Just the dummy description

Demo on 3v4l.org

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.