Parsing XML in PHP.

Caporegime
Joined
12 Mar 2004
Posts
29,919
Location
England
So I have a url and using simplexml it returns an xml string structured like this

Code:
<?xml version='1.0' encoding='UTF-8'?>
<eveapi version="2">
  <currentTime>2013-08-08 19:04:42</currentTime>
  <result>
    <rowset name="assets" key="itemID" columns="itemID,locationID,typeID,quantity,flag,singleton">
      <row itemID="1010603245847" locationID="30001718" typeID="12235" quantity="1" flag="0" singleton="1" rawQuantity="-1">
        <rowset name="contents" key="itemID" columns="itemID,typeID,quantity,flag,singleton">
          <row itemID="1009632688490" typeID="16275" quantity="1352" flag="122" singleton="0" />
        </rowset>
      </row>
      <row itemID="1010714125531" locationID="30001718" typeID="12235" quantity="1" flag="0" singleton="1" rawQuantity="-1">
        <rowset name="contents" key="itemID" columns="itemID,typeID,quantity,flag,singleton">
          <row itemID="1010714202903" typeID="4247" quantity="332" flag="0" singleton="0" />
          <row itemID="1011057346534" typeID="16275" quantity="1000" flag="122" singleton="0" />
        </rowset>
      </row>
      <row itemID="1005071864459" locationID="60012553" typeID="3243" quantity="1" flag="4" singleton="1" rawQuantity="-1" />
      <row itemID="171486341" locationID="66009086" typeID="27" quantity="1" flag="83" singleton="1" rawQuantity="-1">
        <rowset name="contents" key="itemID" columns="itemID,typeID,quantity,flag,singleton">
          <row itemID="220986419" typeID="1945" quantity="1" flag="120" singleton="1" rawQuantity="-1" />
          <row itemID="590694203" typeID="1145" quantity="1" flag="4" singleton="1" rawQuantity="-1" />
          <row itemID="1010639012203" typeID="896" quantity="1" flag="4" singleton="0" />
          <row itemID="1010639006684" typeID="889" quantity="1" flag="4" singleton="0" />
          <row itemID="1010639009068" typeID="898" quantity="1" flag="4" singleton="0" />
          <row itemID="1010637648726" typeID="1156" quantity="1" flag="4" singleton="0" />
          <row itemID="1011106225028" typeID="952" quantity="1" flag="120" singleton="1" rawQuantity="-2" />
          <row itemID="1011106225030" typeID="952" quantity="1" flag="120" singleton="1" rawQuantity="-2" />
          <row itemID="1011106225788" typeID="956" quantity="1" flag="120" singleton="1" rawQuantity="-2" />
          <row itemID="1011106225789" typeID="956" quantity="1" flag="120" singleton="1" rawQuantity="-2" />
          <row itemID="1011106225790" typeID="956" quantity="1" flag="120" singleton="1" rawQuantity="-2" />
          <row itemID="1011443092085" typeID="4315" quantity="1" flag="117" singleton="1" rawQuantity="-2" />
          <row itemID="1011443092086" typeID="4315" quantity="1" flag="117" singleton="1" rawQuantity="-2" />
          <row itemID="1011443092087" typeID="4315" quantity="1" flag="117" singleton="1" rawQuantity="-2" />
          <row itemID="1011443092093" typeID="4315" quantity="1" flag="117" singleton="1" rawQuantity="-2" />
        </rowset>
      </row>
    </rowset>
  </result>
  <cachedUntil>2013-08-09 01:04:42</cachedUntil>
</eveapi>

I want to put just the itemId in my database table where (quantity=1 && singleton=0) || rawQuantity=-1).

Can anyone give me any hints here? I'm more used to working with xpath and stuff in java.
 
Last edited:
Associate
Joined
21 May 2003
Posts
1,365
You can use xpath with SimpleXML as well: http://www.php.net/manual/en/simplexmlelement.xpath.php

Here's a way to do it with recursion...

PHP:
<?php
$xml = simplexml_load_file('test.xml');
$itemIds = array();
getItemIds($xml->result, $itemIds);
print_r($itemIds);

/**
 * Recursively build a list of item ids by traversing nested rows.
 *
 * @param SimpleXMLElement $row
 * @param array            $itemIds
 */
function getItemIds($row, &$itemIds) {
    if (isset($row->rowset)) {
        foreach ($row->rowset->row as $innerRow) {
            getItemIds($innerRow, $itemIds);
        }
    }

    // attributes are simpleXML objects until cast
    $attr = $row->attributes();
    $itemID = (string) $attr['itemID'];
    $quantity = (int) $attr['quantity'];
    $rawQuantity = (int) $attr['rawQuantity'];
    $singleton = (int) $attr['singleton'];

    if (($quantity === 1 && $singleton === 0) || $rawQuantity === -1) {
        $itemIds[] = $itemID;
    }
}
 
Last edited:
Back
Top Bottom