1

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

hakre
  • 193,403
  • 52
  • 435
  • 836
greek59
  • 13
  • 1
  • 3

4 Answers4

2

This is a great use case for XPath:

$search = $simplexml->xpath('/Tables/Table[name="I_EXCDATERESA"]/Row/Field[name="BOOKNR"]');
davidethell
  • 11,708
  • 6
  • 43
  • 63
1

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

Community
  • 1
  • 1
Vad.Gut
  • 521
  • 1
  • 5
  • 19
1

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!

hakre
  • 193,403
  • 52
  • 435
  • 836
ab_dev86
  • 1,952
  • 16
  • 21
  • Ok! so just because you are new to Stack Overflow accept the answer as the correct one, mine and also yours reputation will improve. Thanks :-) – ab_dev86 Apr 14 '12 at 14:59
  • There also is a logical flaw on testing for the table name, it should be done right after the tablename is read, not inside a deeper level of indentation. – hakre Oct 26 '13 at 20:47
0
$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"]');
Chicharito
  • 11
  • 4