Searching XML file

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
leewad
Forum Commoner
Posts: 91
Joined: Tue May 11, 2004 8:32 am

Searching XML file

Post by leewad »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi

I have a XML file which is way too big to parser ( 417MB ) so i`m wanting to create a class so i can return the values of a given search.

so say the XML was:

[syntax="xml"]
<houses>
<house>
<code>PT1234</code>
<rooms>3</rooms>
<price>100000</price>
</house>
<house>
<code>ABCD12</code>
<rooms>7</rooms>
<price>200000</price>
</house>
<house>
<code>XYZ12</code>
<rooms>1</rooms>
<price>50000</price>
</house>
<house>
<code>PYT122</code>
<rooms>6</rooms>
<price>56000</price>
</house>
</houses>
Would it be possible to return all the node information for a given search, so for example:[/syntax]

Code: Select all

getinfo(ABCD12);
which would return

Code: Select all

$code = "ABCD12";
$rooms=7;
$price= 200000;

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
Rovas
Forum Contributor
Posts: 272
Joined: Mon Aug 21, 2006 7:09 am
Location: Romania

Post by Rovas »

Yes it is possible but you have to work a lot to achieve it: you' ll still need to parse the xml file in transforming it in a more search friendly type array and the use a function to search and to display the result.
Take a look at the first contributor note on this page: http://www.php.net/manual/en/function.x ... option.php and at this page: http://www.php.net/manual/en/function.array-search.php.
Good luck!
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

Or use xpath with simplexml

Code: Select all

<?php
$xml = '<houses>
<house>
<code>PT1234</code>
<rooms>3</rooms>
<price>100000</price>
</house>
<house>
<code>ABCD12</code>
<rooms>7</rooms>
<price>200000</price>
</house>
<house>
<code>XYZ12</code>
<rooms>1</rooms>
<price>50000</price>
</house>
<house>
<code>PYT122</code>
<rooms>6</rooms>
<price>56000</price>
</house>
</houses>';

$doc = simplexml_load_string($xml);
foreach ($doc->xpath('//house[code="XYZ12"]') as $house) {
	echo $house->code, "<br />\n",
		$house->rooms, "<br />\n",
		$house->price, "<br />\n";
}
?>
see http://de2.php.net/simplexml
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

don't use xpath with simplexml, unless you want to give php a memory limit of really massive. sax (xml parser in php) allows you to parse a file in chucks loading a certain number of bytes at a time. It will be slow though.

As the XML has a very simple structure you should put this in a database as soon as you can.
Post Reply