Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / New
Stats: 3,150,839 members, 7,810,223 topics. Date: Saturday, 27 April 2024 at 12:43 AM

Reading An Xml, Using Php ? - Webmasters - Nairaland

Nairaland Forum / Science/Technology / Webmasters / Reading An Xml, Using Php ? (828 Views)

Free Xmlprov Xml Prov Creator Maker And Generator / Create Ajax Chat & Learn Web Page Data Xchange via Json Vs Xml / Writing A Time Based Expiration Script Or Function Using Php And Mysql (2) (3) (4)

(1) (Reply) (Go Down)

Reading An Xml, Using Php ? by sayhi2ay(m): 6:30pm On Oct 08, 2010
<Document>

<PubDate>2010-10-10</PubDate>


<Categories><![CDATA[Animals]]></Categories>

<Authors><![CDATA[<p>Ay Dot Bleep@yahoo.com</p>]]></Authors>

<Headline><![CDATA[<p>This is a 9th headline</p>]]></Headline>


<Excerpt><![CDATA[<p>This is a test story. This is a test story. </p>]]></Brief>

<Body><![CDATA[<p>This is a test story. </p> <p>This is a test story. </p> <p>This is a test story. </p> <p>This is a test story. </p> ]]></Body>

<Files>
<File>
<FileID>55365</FileID>
<FileName><![CDATA[foxy.jpg]]></FileName>
<FileDescription><![CDATA[This is a fox]]></FileDescription>
<FileType>image/jpeg</FileType>
<FileBytes>256</FileBytes>
<FileDateTime>2010-10-05 16:32:27</FileDateTime>
</File>
<File>
<FileID>55370</FileID>
<FileName><![CDATA[catty.jpg]]></FileName>
<FileDescription><![CDATA[This is a cat]]></FileDescription>
<FileType>image/jpeg</FileType>
<FileBytes>512</FileBytes>
<FileDateTime>2010-10-05 16:33:04</FileDateTime>
</File>
<File>
<FileID>55371</FileID>
<FileName><![CDATA[doggy.jpg]]></FileName>
<FileDescription><![CDATA[This is a dog]]></FileDescription>
<FileType>image/jpeg</FileType>
<FileBytes>1024</FileBytes>
<FileDateTime>2010-10-05 16:33:06</FileDateTime>
</File>
</Files>
</Document>




My code:

$xml = new XMLReader();
$xml->open('filename.xml');

$article = array();
while($xml->read())
{

if($xml->nodeType == XMLReader::ELEMENT)
{ $name = $xml->name; }

else if($xml->nodeType == XMLReader::CDATA)
{$article[$name] = $xml->value; }

else if($xml->nodeType == XMLReader::TEXT)
{ $article[$name] = $xml->value;}

}

print_r ($article);
$xml->close();




Result:


Array (
[PubDate] => 2010-10-10
[Categories] => Animals
[Authors] =>Ay Dot Bleep@yahoo.com
[Headline] =>This is a 9th headline
[Excerpt] =>This is a test story. This is a test story. This is a test story.
[Body] =>
This is a test story. This is a test story. This is a test story. This is a test story. This is a test story. This is a test story. This is a test story. This is a test story. This is a test story. This is a test story. This is a test story.

[FileID] => 55371 [FileName] => doggy.jpg [FileDescription] => This is a dog [FileType] => image/jpeg [FileBytes] => 1024 [FileDateTime] => 2010-10-05 16:33:06 )


Challenge:

I am missing the first 2 files. -- fileID, fileName etc etc

been a busy week, cant even think straight, any thoughts?

thanks.
Re: Reading An Xml, Using Php ? by sayhi2ay(m): 6:37pm On Oct 08, 2010
btw - my first 'if' statement is not necessary, was trying to loop with it, but not anymore.

my xml file would ALWAYS have an element, so the first 'if' statement will ALWAYS be true, meaning $article[$name] is valid at ALL times.

thanks again.
Re: Reading An Xml, Using Php ? by Nobody: 11:47am On Oct 12, 2010
Na wa o, see plenty codes mehn!
Re: Reading An Xml, Using Php ? by sayhi2ay(m): 2:28pm On Oct 12, 2010
@dhtml: yes o

correction: the first loop is necessary as the $xml->nodeType will forever be assigned XMLReader::ELEMENT, and therefore, the other 'if' statements wont be touched.

Thanks anyway, solved my challenge.
Re: Reading An Xml, Using Php ? by Nobody: 6:46am On Oct 14, 2010
Seriously, do you know that php can analyze html structure just like DOM.
for instance:
<html>
<head>
<title>Lol</title>
</head>
<body bgcolor="blue">

<img src="yahoo.jpg"/>

<img src="yahoo.jpg"/>

</body>
</html>
Something like that, php can take it and get the src of all images, attribute of any element. . . . .

Glad you have solved your problem, i did not take time to read this thread properly when i first came
Re: Reading An Xml, Using Php ? by sayhi2ay(m): 3:47pm On Oct 14, 2010
yea, but number of files is dynamic,

(1) (Reply)

Shared Web Hosting, Resellers Hosting And Master Resellers Hosting / Web4africa Probem / How Do I Convert From Innodb To Myisam?

(Go Up)

Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health
religion celebs tv-movies music-radio literature webmasters programming techmarket

Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10)

Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 19
Disclaimer: Every Nairaland member is solely responsible for anything that he/she posts or uploads on Nairaland.