Solved

use php xmlReader to read subelements

Posted on 2010-09-15
6
696 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
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
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
Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

 
LVL 108

Accepted Solution

by:
Ray Paseur earned 500 total points
Comment Utility
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
Comment Utility
Thanks Ray,
I feel better now that someone else agrees that this was difficult to parse;)
0
 
LVL 108

Expert Comment

by:Ray Paseur
Comment Utility
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
scandir() not reads all files 4 24
PHP and Soap 3 25
how to resize preview image 4 27
How do I edit this Drupal page? 9 26
There is an easy way, in .NET, to centralize the treatment of all unexpected errors. First of all, instead of launching the application directly in a Form, you need first to write a Sub called Main, in a module. Then, set the Startup Object to th…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
This video teaches viewers about errors in exception handling.
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.

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now