Solved

use php xmlReader to read subelements

Posted on 2010-09-15
6
707 Views
Last Modified: 2013-12-12
reading the attributes from the elements works fine, but I run into repeating subelements

What is the basic logic for dealing with that?
while ($reader->read()) {
        switch ($reader->nodeType){
        case (XMLReader::ELEMENT):
                if($reader->localName == 'Rpt') {
                $RptID = $reader->getAttribute("RptID")
     
                }
                elseif($reader->localName == 'Inst') {
                $Exc = $reader->getAttribute("Exc")
                }

Open in new window

0
Comment
Question by:mcgilljd
  • 3
  • 2
6 Comments
 
LVL 11

Expert Comment

by:Amar Bardoliwala
ID: 33688930
Hello mcgilljd,

You did not give your whole code.. So it is difficult to understand your problem.

Can you please give your code that you are executing?

Also please make sure that you are putting "break;"  for each case in your code.

Thanks.
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 33690268
XMLReader might not be the best tool.  Please post your XML test file and I'll show you how I would get the information.  Thanks, ~Ray
0
 

Author Comment

by:mcgilljd
ID: 33691877
Unfortunately, It needs to use xmlReader.
I know xmlsimple would be easier, but this is actually a large file, even though I am showing 1 test record.

I have break statements after each case, I just want some guidance on capturing the sub elements and their attributes.

There is some repition that is throwing me off
<TESTXML t="3456789">
<Batch>
<Alloc RptID="57475770"  ><Instrmt  Src="8" /><Pty ID="ABC" R="21" /><Pty ID="00123" R="1" > <Sub ID="X" Typ="26"/></Pty><Pty ID="ABC123" R="24"/><Amt Amt="12345"/><AllocInner  Qty="1"><Pty ID="ABC" R="21" /><Pty ID="789" R="18" > <Sub ID="V" Typ="26"/></Pty><Pty ID="123ABC" R="24"/><MiscFees Amt="0.03" Typ="13"/></AllocInner></Alloc>
</Batch>
</TESTXML>

Open in new window

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 110

Accepted Solution

by:
Ray Paseur earned 500 total points
ID: 33692229
This is the most amazingly structured XML I have ever seen.  No wonder you found XMLReader to be difficult to use.  It's basically a cursor, and you have to keep track of your own nesting levels.  Sheesh!

It's not even easy to work with SimpleXML.

Is there anything you can do about the structure of this XML thing?  
<?php // RAY_temp_mcgilljd.php
error_reporting(E_ALL);
echo "<pre>";

// TEST DATA
$xml = <<<XML
<TESTXML t="3456789">
  <Batch>
    <Alloc RptID="57475770"  >
      <Instrmt  Src="8" />
      <Pty ID="ABC" R="21" />
      <Pty ID="00123" R="1" >
        <Sub ID="X" Typ="26"/>
      </Pty>
      <Pty ID="ABC123" R="24"/>
      <Amt Amt="12345"/>
      <AllocInner  Qty="1">
        <Pty ID="ABC" R="21" />
        <Pty ID="789" R="18" >
          <Sub ID="V" Typ="26"/>
        </Pty>
        <Pty ID="123ABC" R="24"/>
        <MiscFees Amt="0.03" Typ="13"/>
      </AllocInner>
    </Alloc>
  </Batch>
</TESTXML>
XML;

// MAKE AN OBJECT
$obj = SimpleXML_Load_String($xml);

// ACTIVATE THIS TO SEE THE OBJECT
var_dump($obj);

// USE THE ITERATORS TO FIND ELEMENTS OF THE OBJECT
$i = '';
foreach ($obj->Batch->Alloc as $a)
{
    $r = $a["RptID"];
    foreach ($a->Pty as $p)
    {
        $i .= $p["ID"];
        if (isset($p->Sub["ID"])) $i .= '(' . $p->Sub["ID"] . ')';
        $i .= ' ';
    }
}
echo PHP_EOL . 'Alloc:' . PHP_EOL . $r . ' ' . $i;

$i = '';
foreach ($obj->Batch->Alloc->AllocInner as $a)
{
    $r = $a["Qty"];
    foreach ($a->Pty as $p)
    {
        $i .= $p["ID"];
        if (isset($p->Sub["ID"])) $i .= '(' . $p->Sub["ID"] . ')';
        $i .= ' ';
    }
}
echo PHP_EOL . 'AllocInner:' . PHP_EOL . $r . ' ' . $i;

Open in new window

0
 

Author Closing Comment

by:mcgilljd
ID: 33692349
Thanks Ray,
I feel better now that someone else agrees that this was difficult to parse;)
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 33692394
Yeah, it borders on astonishing!  

If the existing file is too big to load with SimpleXML, maybe the publisher could be convinced to send the file in segments, just a thought.

Thanks for the points and good luck with it, ~Ray
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Link failure 16 66
Compiling PHP with Curl plus protocols 8 23
How do I post more than 1 item to php backend 24 30
Delete image(s) associated with record(s) 16 26
When we want to run, execute or repeat a statement multiple times, a loop is necessary. This article covers the two types of loops in Python: the while loop and the for loop.
This article discusses four methods for overlaying images in a container on a web page
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question