Solved

use php xmlReader to read subelements

Posted on 2010-09-15
6
703 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 109

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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 109

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 109

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
The purpose of this article is to demonstrate how we can use conditional statements using Python.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

770 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