Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

use php xmlReader to read subelements

Posted on 2010-09-15
6
Medium Priority
?
719 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 111

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
Technology Partners: 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 111

Accepted Solution

by:
Ray Paseur earned 2000 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 111

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
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.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

597 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