473,405 Members | 2,154 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

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
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <Date TEXT="Wed, Jan 13, 10">
  3.     <Event GAME="Hockey">
  4.         <Competitor NAME="TEAM 1"></Competitor>
  5.         <Competitor NAME="TEAM 2"></Competitor>
  6.         <Time TEXT="9:05p EST"/>
  7.     </Event>
  8.     <Event GAME="Hockey">
  9.         <Competitor NAME="TEAM 3"></Competitor>
  10.         <Competitor NAME="TEAM 4"></Competitor>
  11.         <Time TEXT="8:05p EST"/>
  12.     </Event>
  13. </Date>
PHP
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $xml = new XMLReader();
  3. $xml->open('test.xml');
  4.  
  5. while ($xml->read()) {
  6.     // what's the best way to do this??
  7. }    
  8. $xml->close();
  9. ?> 
Desired output HMTL:
Expand|Select|Wrap|Line Numbers
  1. <b>Wed, Jan 13, 10 at 8:05p EST</b><br />
  2. TEAM 3<br />
  3. TEAM 4<br />
  4. <hr>
  5.  
  6. <b>Wed, Jan 13, 10 at 9:05p EST</b><br />
  7. TEAM 1<br />
  8. TEAM 2<br />
  9. <hr>
Thanks!!
Jan 18 '10 #1
4 4304
Dormilich
8,658 Expert Mod 8TB
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.
Jan 18 '10 #2
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!
Jan 18 '10 #3
Dormilich
8,658 Expert Mod 8TB
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.
Jan 18 '10 #4
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
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <Schedule>
  3.     <Date TEXT="Wed, Jan 14, 10">
  4.         <Event GAME="Hockey">
  5.             <Competitor NAME="TEAM 1"></Competitor>
  6.             <Competitor NAME="TEAM 2"></Competitor>
  7.             <Time TEXT="9:05p EST"/>
  8.         </Event>
  9.         <Event GAME="Hockey">
  10.             <Competitor NAME="TEAM 3"></Competitor>
  11.             <Competitor NAME="TEAM 4"></Competitor>
  12.             <Time TEXT="8:05p EST"/>
  13.         </Event>
  14.     </Date>
  15.     <Date TEXT="Wed, Jan 13, 10">
  16.         <Event GAME="Hockey">
  17.             <Competitor NAME="TEAM 5"></Competitor>
  18.             <Competitor NAME="TEAM 6"></Competitor>
  19.             <Time TEXT="9:35p EST"/>
  20.         </Event>
  21.         <Event GAME="Hockey">
  22.             <Competitor NAME="TEAM 7"></Competitor>
  23.             <Competitor NAME="TEAM 8"></Competitor>
  24.             <Time TEXT="6:35p EST"/>
  25.         </Event>
  26.     </Date>
  27. </Schedule>
PHP
Expand|Select|Wrap|Line Numbers
  1. $xml = new XMLReader();
  2. $xml->open('test.xml');
  3.  
  4. $a = 0;
  5. $b = 0;
  6. $lines = array();
  7. while ($xml->read()) {
  8.     switch ($xml->nodeType) {
  9.         case (XMLReader::ELEMENT):
  10.  
  11.             if ($xml->localName == 'Date') {
  12.                 $Date_array = array('TEXT' => $xml->getAttribute('TEXT'));
  13.                 $lines['Date'][$a]['attr'] = $Date_array;
  14.             }
  15.  
  16.             if ($xml->localName == 'Event') {
  17.                 $Event_array = array('GAME' => $xml->getAttribute('GAME'));
  18.                 $lines['Date'][$a]['Event'][$b]['attr'] = $Event_array;
  19.             }
  20.  
  21.             if ($xml->localName == 'Competitor') {
  22.                 $Competitor_array = array('NAME' => $xml->getAttribute('NAME'));
  23.                 $lines['Date'][$a]['Event'][$b]['Competitor'][]['attr'] = $Competitor_array;
  24.             }
  25.  
  26.             if ($xml->localName == 'Time') {
  27.                 $Time_array = array('TEXT' => $xml->getAttribute('TEXT'));
  28.                 $lines['Date'][$a]['Event'][$b]['Time']['attr'] = $Time_array;
  29.             }
  30.  
  31.         break;
  32.         case (XMLReader::END_ELEMENT):
  33.  
  34.             if ($xml->localName == 'Date') {
  35.                 $a++;
  36.             }
  37.  
  38.             if ($xml->localName == 'Event') {
  39.                 $b++;
  40.             }
  41.  
  42.         break;
  43.     }
  44. }    
  45. $xml->close();
  46.  
  47. echo '<pre>';
  48. print_r($lines);
  49. echo '</pre>';
And here's the array I get
Expand|Select|Wrap|Line Numbers
  1. Array
  2. (
  3.     [Date] => Array
  4.         (
  5.             [0] => Array
  6.                 (
  7.                     [attr] => Array
  8.                         (
  9.                             [TEXT] => Wed, Jan 14, 10
  10.                         )
  11.  
  12.                     [Event] => Array
  13.                         (
  14.                             [0] => Array
  15.                                 (
  16.                                     [attr] => Array
  17.                                         (
  18.                                             [GAME] => Hockey
  19.                                         )
  20.  
  21.                                     [Competitor] => Array
  22.                                         (
  23.                                             [0] => Array
  24.                                                 (
  25.                                                     [attr] => Array
  26.                                                         (
  27.                                                             [NAME] => TEAM 1
  28.                                                         )
  29.  
  30.                                                 )
  31.  
  32.                                             [1] => Array
  33.                                                 (
  34.                                                     [attr] => Array
  35.                                                         (
  36.                                                             [NAME] => TEAM 2
  37.                                                         )
  38.  
  39.                                                 )
  40.  
  41.                                         )
  42.  
  43.                                     [Time] => Array
  44.                                         (
  45.                                             [attr] => Array
  46.                                                 (
  47.                                                     [TEXT] => 9:05p EST
  48.                                                 )
  49.  
  50.                                         )
  51.  
  52.                                 )
  53.  
  54.                             [1] => Array
  55.                                 (
  56.                                     [attr] => Array
  57.                                         (
  58.                                             [GAME] => Hockey
  59.                                         )
  60.  
  61.                                     [Competitor] => Array
  62.                                         (
  63.                                             [0] => Array
  64.                                                 (
  65.                                                     [attr] => Array
  66.                                                         (
  67.                                                             [NAME] => TEAM 3
  68.                                                         )
  69.  
  70.                                                 )
  71.  
  72.                                             [1] => Array
  73.                                                 (
  74.                                                     [attr] => Array
  75.                                                         (
  76.                                                             [NAME] => TEAM 4
  77.                                                         )
  78.  
  79.                                                 )
  80.  
  81.                                         )
  82.  
  83.                                     [Time] => Array
  84.                                         (
  85.                                             [attr] => Array
  86.                                                 (
  87.                                                     [TEXT] => 8:05p EST
  88.                                                 )
  89.  
  90.                                         )
  91.  
  92.                                 )
  93.  
  94.                         )
  95.  
  96.                 )
  97.  
  98.             [1] => Array
  99.                 (
  100.                     [attr] => Array
  101.                         (
  102.                             [TEXT] => Wed, Jan 13, 10
  103.                         )
  104.  
  105.                     [Event] => Array
  106.                         (
  107.                             [2] => Array
  108.                                 (
  109.                                     [attr] => Array
  110.                                         (
  111.                                             [GAME] => Hockey
  112.                                         )
  113.  
  114.                                     [Competitor] => Array
  115.                                         (
  116.                                             [0] => Array
  117.                                                 (
  118.                                                     [attr] => Array
  119.                                                         (
  120.                                                             [NAME] => TEAM 5
  121.                                                         )
  122.  
  123.                                                 )
  124.  
  125.                                             [1] => Array
  126.                                                 (
  127.                                                     [attr] => Array
  128.                                                         (
  129.                                                             [NAME] => TEAM 6
  130.                                                         )
  131.  
  132.                                                 )
  133.  
  134.                                         )
  135.  
  136.                                     [Time] => Array
  137.                                         (
  138.                                             [attr] => Array
  139.                                                 (
  140.                                                     [TEXT] => 9:35p EST
  141.                                                 )
  142.  
  143.                                         )
  144.  
  145.                                 )
  146.  
  147.                             [3] => Array
  148.                                 (
  149.                                     [attr] => Array
  150.                                         (
  151.                                             [GAME] => Hockey
  152.                                         )
  153.  
  154.                                     [Competitor] => Array
  155.                                         (
  156.                                             [0] => Array
  157.                                                 (
  158.                                                     [attr] => Array
  159.                                                         (
  160.                                                             [NAME] => TEAM 7
  161.                                                         )
  162.  
  163.                                                 )
  164.  
  165.                                             [1] => Array
  166.                                                 (
  167.                                                     [attr] => Array
  168.                                                         (
  169.                                                             [NAME] => TEAM 8
  170.                                                         )
  171.  
  172.                                                 )
  173.  
  174.                                         )
  175.  
  176.                                     [Time] => Array
  177.                                         (
  178.                                             [attr] => Array
  179.                                                 (
  180.                                                     [TEXT] => 6:35p EST
  181.                                                 )
  182.  
  183.                                         )
  184.  
  185.                                 )
  186.  
  187.                         )
  188.  
  189.                 )
  190.  
  191.         )
  192.  
  193. )
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??
Jan 18 '10 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Bob Rosen | last post by:
My message concerns a sample application that I took verbatim from the book titled "SAMS Teach Yourself Visual Basic.NET Web Programming in 21 days". It consists of a web page that takes the names...
11
by: Ben | last post by:
Hi, I'm just moving to VB.NET and I'm trying to load a recordset intoan XMLReader and loop through the records. When I use the.ReadElementString() method to get the next 'record' if it hasreached...
0
by: John R. | last post by:
I have a class that I am serializing. I put on the properties I don't want to serialize. I want to deserialize it to itself like this: XmlSerializer deserializer = new...
0
by: Jen | last post by:
My main question: "How can I get a TextReader or Stream object from an existing XmlReader?". Read on for more: I have an existing XmlReader. Let's call it reader1. I'm creating another reader,...
1
by: Peter Nofelt | last post by:
Hey All, I am having issue with serializing a class and its base class using ..net 2.0. I am using IXmlSerializable I've provided code displaying my at the bottom of this post. Thanks ahead...
1
by: Daniel | last post by:
Does system.xml have any way to transofrm data with an xswl style sheet using strings like MSXML2 does? how to convert this to use System.XML so i do not depend on MSXML2 interop? static...
2
by: Jeff Calico | last post by:
Hello all. I am implementing a SAX filter to strip a bunch of unneeded elements out of a large XML file. I found a book "Java & XML" by Brett McLaughlin, and an interesting article by him wich...
0
by: PSingh | last post by:
Hi, I know this is a frequently asked question but have tried several combinations and cant seem to figure this out. I am serializing my object as follows: XmlSerializer serializer = new...
1
by: engwar | last post by:
The title says it all. How do I use XMLDocument.Validate() against a DTD. In all my Googling about this I only find examples of how to validate an XMLReader or XmlValidatingReader against a DTD....
3
by: Adhal | last post by:
Hello, I have an XML and XSD file. When I do validation in the XML file I only get the first error. Is it possible to list all errors? One other question. -------------Microsoft Sample Code...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.