₦airaland Forum

Welcome, Guest: RegisterLoginWith GoogleTrendingRecentNew

Stats: 3,326,301 members, 8,425,946 topics. Date: Saturday, 13 June 2026 at 11:37 AM

Toggle theme

Reading An Xml, Using Php ? - Webmasters - Nairaland

Nairaland ForumScience/TechnologyWebmastersReading An Xml, Using Php ? (942 Views)

1 Reply (Go Down)

Reading An Xml, Using Php ? by sayhi2ay(op): 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(op): 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(op): 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(op): 3:47pm On Oct 14, 2010
yea, but number of files is dynamic,
1 Reply

Free Xmlprov Xml Prov Creator Maker And GeneratorCreate Ajax Chat & Learn Web Page Data Xchange via Json Vs XmlWriting A Time Based Expiration Script Or Function Using Php And Mysql234

The Webmaster Section Is The Most Boring On NairalandWhere In The World Is Omnipotens?Joomla Tutorial Video _ Free Delivery Within Lagos