Page 1 of 1

Searching XML file

Posted: Wed Dec 27, 2006 6:42 am
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]

Posted: Wed Dec 27, 2006 7:49 am
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!

Posted: Wed Dec 27, 2006 12:49 pm
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

Posted: Wed Dec 27, 2006 10:31 pm
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.