greek59
greek59

Reputation: 13

php simplexml find value of a specific field

The XML, I have:

<?xml version="1.0" encoding="windows-1252" ?> 
    - <Tables>
        - <Table name="I_BOOKING">
            - <Row action="UPDATE" success="Y">
              <Field name="AUTOBOOK">888800</Field> 
              <Field name="TOUROP">01</Field> 
              <Field name="REFERENCE">GSDFFD</Field> 
              <Field name="NBPAX">2</Field> 
              <Field name="NBINF">1</Field> 
              </Row>
      </Table>
      - <Table name="I_EXCDATERESA">
          - <Row action="UPDATE" success="Y">
               <Field name="EXCURS">KNO</Field> 
               <Field name="DATE">2012-04-12</Field> 
               <Field name="BOOKNR">125445</Field> 
               <Field name="NAME">TEST 12/4</Field> 
               <Field name="PICKUPTIME">00:00:00</Field> 
           </Row>
         - <Row action="UPDATE" success="Y">
               <Field name="EXCURS">KNO</Field> 
               <Field name="DATE">2012-04-13</Field> 
               <Field name="BOOKNR">14574575</Field> 
               <Field name="NAME">TEST 13/4</Field> 
               <Field name="PICKUPTIME">00:00:00</Field> 
          </Row>
       </Table>
    </Tables>

When I treat the table I_EXCDATERESA, I need to get the value of Field BOOKNR, so 125445 or 14574575 in this example, according to the row I am dealing with and load it in $autobook:

...

$simplexml = simplexml_import_dom($dom);

foreach ($simplexml->Table as $value)
{
    $tableName = $value->attributes()->name;
    foreach ($value->Row as $value)
    {
        if ($tableName == 'I_EXCDATERESA')
        {
            if ($value->Field->attributes()->name == 'BOOKNR')
            {
            $autoBook = $value->Field; 

This is not working, $autobook is not loaded, as it is not on the first 'Field' but on the third

Upvotes: 1

Views: 2604

Answers (4)

ab_dev86
ab_dev86

Reputation: 1982

Test this, it worked for me:

foreach ($simplexml->Table as $value)
{
    $tableName = $value->attributes()->name;
    foreach ($value->Row as $value_1)
    {
        if ($tableName == 'I_EXCDATERESA')
        {
            foreach ($value_1->Field as $value_2)
            {
                if ($value_2->attributes()->name == 'BOOKNR')
                {
                    $autoBook = $value_2[0];
                    echo $autoBook;
                }
            }
        }
    }
}

Your problem is with:

 $value->Field->attributes()->name=='BOOKNR'

That's too much: I added another cycle on each field of elemts Row!

Upvotes: 1

Chicharito
Chicharito

Reputation: 11

$simplexml->xpath('/Tables/Table[name="I_EXCDATERESA"]/Row/Field[name="BOOKNR"]');

does'nt work, you missed the @

$simplexml->xpath('/Tables/Table[@name="I_EXCDATERESA"]/Row/Field[@name="BOOKNR"]');

Upvotes: 0

davidethell
davidethell

Reputation: 12018

This is a great use case for XPath:

$search = $simplexml->xpath('/Tables/Table[name="I_EXCDATERESA"]/Row/Field[name="BOOKNR"]');

Upvotes: 2

Vad.Gut
Vad.Gut

Reputation: 531

well there are couple of ways to approach this situation

  1. using DOM

  2. using the attribute() function inside simplexml which was covered here

  3. and probably the easiest option is noticing that BOOKNER is the third node in every Row I would do something like this.

    for ($i = 0; $i < count($simplexml->->children()); $i++){ echo $simplexml->Table[$i]->Table->Field[2]; }

good luck

Upvotes: 1

Related Questions