Jump to content

searching XML using XMLReader


nadeemshafi9

Recommended Posts

my current function can only output the entire XML document

 

i am having a hard time trying to searhc the titles in the XML and then output node that contains the search arg

 

i need to mod the below to only output if title contains args, i have tried a few ways but have reverted to this full output, ihave tried moving back and forward with no success

 

<?php
final public static function all( $args ){
	$xml = new XMLReader(); 
	$xml->open('music.xml');

	echo "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>";

                         // i need to use args to limit the output to  the nodes that contain args in there title

	while ($xml->read()) {
		if($xml->nodeType == XMLREADER::ELEMENT) echo "<".$xml->localName.">";
		echo $xml->value;
		if($xml->nodeType == XMLREADER::END_ELEMENT) echo "</".$xml->localName.">";
	}
	$xml->close();
}
?>

 

 

xml file

 

<?xml version="1.0" encoding="UTF-8" ?>
<music>
<file name="1">
	<title>1</title>
	<filename>1.mp3</filename>
</file>
<file name="2">
	<title>2</title>
	<filename>2.mp3</filename>
</file>
<file name="3">
	<title>3</title>
	<filename>3.mp3</filename>
</file>
<file name="4">
	<title>4</title>
	<filename>4.mp3</filename>
</file>
</music>

 

any help much appreciated

Link to comment
https://forums.phpfreaks.com/topic/146773-searching-xml-using-xmlreader/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.