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 - <?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 - <?php
-
$xml = new XMLReader();
-
$xml->open('test.xml');
-
-
while ($xml->read()) {
-
// what's the best way to do this??
-
}
-
$xml->close();
-
?>
Desired output HMTL: - <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!!
4 4304
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.
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!
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.
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 - <?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 - $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
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??
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
|
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...
|
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....
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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...
|
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...
|
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,...
|
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...
|
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...
|
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...
| |