How to order XML while using XmlReader Class...

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Scooby10
    New Member
    • Jan 2010
    • 5

    How to order XML while using XmlReader Class...

    I'm just learning about the XmlReader Class and I've been racking my brain on this for quite some time and just cant seem to get anywhere because I don't know the proper way to do so..

    I'm trying to read the xml using the XmlReader Class but I would also like to order the events by date, if possible, and then display them as html..

    Here is a small example of what I'm looking for:

    XML
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <Date TEXT="Wed, Jan 13, 10">
    	<Event GAME="Hockey">
    		<Competitor NAME="TEAM 1"></Competitor>
    		<Competitor NAME="TEAM 2"></Competitor>
    		<Time TEXT="9:05p EST"/>
    	</Event>
    	<Event GAME="Hockey">
    		<Competitor NAME="TEAM 3"></Competitor>
    		<Competitor NAME="TEAM 4"></Competitor>
    		<Time TEXT="8:05p EST"/>
    	</Event>
    </Date>
    PHP
    Code:
    <?php
    $xml = new XMLReader();
    $xml->open('test.xml');
    
    while ($xml->read()) {
        // what's the best way to do this??
    }    
    $xml->close();
    ?>
    Desired output HMTL:
    Code:
    <b>Wed, Jan 13, 10 at 8:05p EST</b><br />
    TEAM 3<br />
    TEAM 4<br />
    <hr>
    
    <b>Wed, Jan 13, 10 at 9:05p EST</b><br />
    TEAM 1<br />
    TEAM 2<br />
    <hr>
    Thanks!!
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    if you only have such small XML files, you should look at the SimpleXML and DOMDocument classes, their XML processing is easier to understand. you could even consider using XSL-Transformation.

    Comment

    • Scooby10
      New Member
      • Jan 2010
      • 5

      #3
      Hi Dormilich.. Thanks for replying!!

      I did use SimpleXML originally, but my files were taking way to long to load.. I did some searching and came across XmlReader... When loading the same XML file using XmlReader it loads almost instantly, and that's what I'm looking for.. Problem is there's not too many tutorials or examples that I can find so I'm forced to go to the forums.. When I was searching for answers on XmlReader I came across this forum a bunch of times so I figured this is the place to ask..

      The size of my xml files are all different sizes and are much larger than what I posted.. I just posted that small xml as an example because if I can figure that out I think I can take it from there..

      Here's a question I've been wondering..

      For me to order this output by date would you think I need to read the xml data into an array and then order it?? Or is there another way? And, does reading the xml file into an array and then displaying the data slow it down much more than just displaying the output as it's read??

      Thanks so much for any replies!! I've been stuck on this for way too long now!

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        I did use SimpleXML originally, but my files were taking way to long to load.. I did some searching and came across XmlReader... When loading the same XML file using XmlReader it loads almost instantly, and that's what I'm looking for..
        right, that’s because XMLReader reads the data sequentially, while the others load the complete file.

        Problem is there's not too many tutorials or examples that I can find so I'm forced to go to the forums..
        have you already tried the PHP manual entry for XMLReader? often there is valuable information in the comments section.

        a quick look found this.

        For me to order this output by date would you think I need to read the xml data into an array and then order it?? Or is there another way? And, does reading the xml file into an array and then displaying the data slow it down much more than just displaying the output as it's read??
        there are always different ways. but it would be better to put the XML into an array first, since you have to transform the date/time before sorting (which is easier in PHP.

        Comment

        • Scooby10
          New Member
          • Jan 2010
          • 5

          #5
          Yes I have found that.. In fact I think I've tried all the functions on that page.. ha

          Then I guess my other problem is that my xml file has wayyy to many extra attributes that are not needed to be read into the array.. When I run that function with my xml file it takes over 4 seconds to load because of all the extra attributes I do not need to read.. Also, my xml file has no values really.. All the data I'm collecting are attributes..

          I'd like to be able to specify which nodes to read and which attributes to read.. Right now I'm doing it like the below to make my array and read only the info that I need, but it's the worst way ever because I don't have any idea how to do it more efficiently.. (sorry about the space the code takes up)

          XML
          Code:
          <?xml version="1.0" encoding="UTF-8"?>
          <Schedule>
          	<Date TEXT="Wed, Jan 14, 10">
          		<Event GAME="Hockey">
          			<Competitor NAME="TEAM 1"></Competitor>
          			<Competitor NAME="TEAM 2"></Competitor>
          			<Time TEXT="9:05p EST"/>
          		</Event>
          		<Event GAME="Hockey">
          			<Competitor NAME="TEAM 3"></Competitor>
          			<Competitor NAME="TEAM 4"></Competitor>
          			<Time TEXT="8:05p EST"/>
          		</Event>
          	</Date>
          	<Date TEXT="Wed, Jan 13, 10">
          		<Event GAME="Hockey">
          			<Competitor NAME="TEAM 5"></Competitor>
          			<Competitor NAME="TEAM 6"></Competitor>
          			<Time TEXT="9:35p EST"/>
          		</Event>
          		<Event GAME="Hockey">
          			<Competitor NAME="TEAM 7"></Competitor>
          			<Competitor NAME="TEAM 8"></Competitor>
          			<Time TEXT="6:35p EST"/>
          		</Event>
          	</Date>
          </Schedule>
          PHP
          Code:
          $xml = new XMLReader();
          $xml->open('test.xml');
          
          $a = 0;
          $b = 0;
          $lines = array();
          while ($xml->read()) {
              switch ($xml->nodeType) {
                  case (XMLReader::ELEMENT):
          	
          			if ($xml->localName == 'Date') {
          				$Date_array = array('TEXT' => $xml->getAttribute('TEXT'));
          				$lines['Date'][$a]['attr'] = $Date_array;
          			}
          			
          			if ($xml->localName == 'Event') {
          				$Event_array = array('GAME' => $xml->getAttribute('GAME'));
          				$lines['Date'][$a]['Event'][$b]['attr'] = $Event_array;
          			}
          			
          			if ($xml->localName == 'Competitor') {
          				$Competitor_array = array('NAME' => $xml->getAttribute('NAME'));
          				$lines['Date'][$a]['Event'][$b]['Competitor'][]['attr'] = $Competitor_array;
          			}
          			
          			if ($xml->localName == 'Time') {
          				$Time_array = array('TEXT' => $xml->getAttribute('TEXT'));
          				$lines['Date'][$a]['Event'][$b]['Time']['attr'] = $Time_array;
          			}
          	
          		break;
          		case (XMLReader::END_ELEMENT):
          		
          			if ($xml->localName == 'Date') {
          				$a++;
          			}
          			
          			if ($xml->localName == 'Event') {
          				$b++;
          			}
          
          		break;
              }
          }    
          $xml->close();
          
          echo '<pre>';
          print_r($lines);
          echo '</pre>';
          And here's the array I get
          Code:
          Array
          (
              [Date] => Array
                  (
                      [0] => Array
                          (
                              [attr] => Array
                                  (
                                      [TEXT] => Wed, Jan 14, 10
                                  )
          
                              [Event] => Array
                                  (
                                      [0] => Array
                                          (
                                              [attr] => Array
                                                  (
                                                      [GAME] => Hockey
                                                  )
          
                                              [Competitor] => Array
                                                  (
                                                      [0] => Array
                                                          (
                                                              [attr] => Array
                                                                  (
                                                                      [NAME] => TEAM 1
                                                                  )
          
                                                          )
          
                                                      [1] => Array
                                                          (
                                                              [attr] => Array
                                                                  (
                                                                      [NAME] => TEAM 2
                                                                  )
          
                                                          )
          
                                                  )
          
                                              [Time] => Array
                                                  (
                                                      [attr] => Array
                                                          (
                                                              [TEXT] => 9:05p EST
                                                          )
          
                                                  )
          
                                          )
          
                                      [1] => Array
                                          (
                                              [attr] => Array
                                                  (
                                                      [GAME] => Hockey
                                                  )
          
                                              [Competitor] => Array
                                                  (
                                                      [0] => Array
                                                          (
                                                              [attr] => Array
                                                                  (
                                                                      [NAME] => TEAM 3
                                                                  )
          
                                                          )
          
                                                      [1] => Array
                                                          (
                                                              [attr] => Array
                                                                  (
                                                                      [NAME] => TEAM 4
                                                                  )
          
                                                          )
          
                                                  )
          
                                              [Time] => Array
                                                  (
                                                      [attr] => Array
                                                          (
                                                              [TEXT] => 8:05p EST
                                                          )
          
                                                  )
          
                                          )
          
                                  )
          
                          )
          
                      [1] => Array
                          (
                              [attr] => Array
                                  (
                                      [TEXT] => Wed, Jan 13, 10
                                  )
          
                              [Event] => Array
                                  (
                                      [2] => Array
                                          (
                                              [attr] => Array
                                                  (
                                                      [GAME] => Hockey
                                                  )
          
                                              [Competitor] => Array
                                                  (
                                                      [0] => Array
                                                          (
                                                              [attr] => Array
                                                                  (
                                                                      [NAME] => TEAM 5
                                                                  )
          
                                                          )
          
                                                      [1] => Array
                                                          (
                                                              [attr] => Array
                                                                  (
                                                                      [NAME] => TEAM 6
                                                                  )
          
                                                          )
          
                                                  )
          
                                              [Time] => Array
                                                  (
                                                      [attr] => Array
                                                          (
                                                              [TEXT] => 9:35p EST
                                                          )
          
                                                  )
          
                                          )
          
                                      [3] => Array
                                          (
                                              [attr] => Array
                                                  (
                                                      [GAME] => Hockey
                                                  )
          
                                              [Competitor] => Array
                                                  (
                                                      [0] => Array
                                                          (
                                                              [attr] => Array
                                                                  (
                                                                      [NAME] => TEAM 7
                                                                  )
          
                                                          )
          
                                                      [1] => Array
                                                          (
                                                              [attr] => Array
                                                                  (
                                                                      [NAME] => TEAM 8
                                                                  )
          
                                                          )
          
                                                  )
          
                                              [Time] => Array
                                                  (
                                                      [attr] => Array
                                                          (
                                                              [TEXT] => 6:35p EST
                                                          )
          
                                                  )
          
                                          )
          
                                  )
          
                          )
          
                  )
          
          )
          That works for me there, but as you can see in the php code starts to get complicated with writing that array and my xml file has about 4 more levels than this example.. Having a hell of a time..

          Would you happen to know of a simpler way to approach this??

          Comment

          Working...