?
Solved

use php xmlReader to read subelements

Posted on 2010-09-15
6
Medium Priority
?
715 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
The purpose of this article is to demonstrate how we can use conditional statements using Python.
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.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

752 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