Log in

View Full Version : getting xml data using php querystring



yzgulec
04-01-2011, 10:53 PM
Hello,

I have an XML file like this:


<?xml version="1.0"?>
<document>
<symptoms>
<symptom>
<type>general</type>
<area>legs</area>
<title>Ankle Pain</title>
<url>/symptoms/symptom-1.html</url>
<contentID>anklePain</contentID>
</symptom>

<symptom>
<type>female</type>
<area>abdomen_pelvis</area>
<title>Bleeding After Menopause</title>
<url>/symptoms/symptom-2.html</url>
<contentID>BleedingAfterMenopause</contentID>
</symptom>

<symptom>
<type>male</type>
<area>abdomen_pelvis</area>
<title>Blood in the Urine in Men</title>
<url>/symptoms/symptom-3.html</url>
<contentID>bloodintheurineinmen</contentID>
</symptom>
</symptoms>
</document>

I would like to have a PHP script that outputs the exact part of XML file when url is written by a query criteria. For example

www.mydomain.com/script.php?type=male&area=abdomen_pelvis

will output:


<document>
<symptoms>
<symptom>
<type>male</type>
<area>abdomen_pelvis</area>
<title>Blood in the Urine in Men</title>
<url>/symptoms/symptom-3.html</url>
<contentID>bloodintheurineinmen</contentID>
</symptom>
</symptoms>
</document>

and for example if the query criteria is "male": www.mydomain.com/script.php?type=male

the output should contain both general and male tags in xml file ( or if the criteria is "female" it should take "female" and "general" tags):


<document>
<symptoms>
<symptom>
<type>general</type>
<area>legs</area>
<title>Ankle Pain</title>
<url>/symptoms/symptom-1.html</url>
<contentID>anklePain</contentID>
</symptom>

<symptom>
<type>male</type>
<area>abdomen_pelvis</area>
<title>Blood in the Urine in Men</title>
<url>/symptoms/symptom-3.html</url>
<contentID>bloodintheurineinmen</contentID>
</symptom>
</symptoms>
</document>

I am new at PHP, I've tried something but I couldn't get the result I want. I don't know it is a complex problem or not. Can you please help?

Thank you.

traq
04-02-2011, 01:33 AM
I'd recommend looking into the simpleXML extension (http://us2.php.net/manual/en/book.simplexml.php). It's a little complex if you aren't familiar with PHP and Object-Oriented Programming; however, there isn't really a simpler way to deal with XML.

You might consider using a database for this instead. If you're not dealing with a lot of entries, you won't notice much of a problem; but if your XML file gets very large, it can become difficult to manage.

In order to read an XML file, PHP must load the entire file and create an object to hold it. This is not a very efficient way to deal with large amounts of data. A database would give you more flexibility in storing the information, and searching would be much faster as well.

yzgulec
04-02-2011, 09:49 AM
Thanks traq. Do you have any idea bout the codding part for XML only? I don't want to do anything with a db yet.

traq
04-02-2011, 08:09 PM
Basically, SimpleXML takes the entire XML document and creates an object out of it. So, you could take the query string and look for those values:



$xmlfile = '/path/to/file.xml';

$XML = new SimpleXMLElement($xmlfile);

foreach($XML->symptoms->symptom as $symptom){
if($symptom->type == 'male' || $symptom->type == 'general'){
// print out symptoms
}
}


This is just a general example, it won't work as-is. It does show the drawbacks of using this method: you have to load everything and compare everything to find the one entry you want. As database finds it quickly, and you only have to deal with the one entry.

Read through the SimpleXML examples and see if you can figure it out. If you have any specific questions, I'll be happy to help. If you need someone to do it for you, you might consider posting in the paid help forum.

yzgulec
04-02-2011, 11:12 PM
Hi traq, I checked out your website in your signature. I guess you can help me. How can I reach you for professional work and detailed information about the job?

traq
04-02-2011, 11:43 PM
you can use my contact page (http://www.custom-anything.com/contact).

However, be aware that I will recommend the use of a database. :D If a database is simply not an option, we can use xml; but for this purpose a database is the better choice, by a longshot.

djr33
04-03-2011, 12:08 AM
Just to add a comment about what traq is saying: XML can do this, but it will become sluggish at some point and increasingly more difficult to work with. So a database might be slightly harder in some sense, but it will end up being less work to deal with because it won't have the problems that XML would. (In other words, I completely agree with his recommendation.)

If you really want to use XML, you could actually import XML into the database (that would be more work, but it's possible to transfer data between the two). So this could allow you to use your current information or whatever is required.

yzgulec
04-03-2011, 12:28 AM
Just to add a comment about what traq is saying: XML can do this, but it will become sluggish at some point and increasingly more difficult to work with. So a database might be slightly harder in some sense, but it will end up being less work to deal with because it won't have the problems that XML would. (In other words, I completely agree with his recommendation.)

If you really want to use XML, you could actually import XML into the database (that would be more work, but it's possible to transfer data between the two). So this could allow you to use your current information or whatever is required.

Yeah I got it. I think I will use database option. Thank you.

yzgulec
04-03-2011, 09:45 AM
you can use my contact page (http://www.custom-anything.com/contact).

However, be aware that I will recommend the use of a database. :D If a database is simply not an option, we can use xml; but for this purpose a database is the better choice, by a longshot.

I sent you a message thru contact page. Did you get it?

Thanks.

traq
04-03-2011, 04:15 PM
Yes I did; I'm reading it now. :)