View Full Version : Parsing XML with PHP
teajay99
08-27-2009, 01:14 PM
Hi Guys,
I'm afraid i am a complete novice when it comes to XML but know PHP pretty well. I have a form located here.. http://clientel-transit.co.uk/life-assurance/mortgage-protection.php which sends values to a third party server and returns the quote values in XML.
I have also been sent the following file available here.. http://keithdevens.com/software/phpxml/xml.php
Can anyone please give me any pointers on how I can echo the values returned in to a more meaningful format using the above file or perhaps a different way?
Thanks in advance :)
JasonDFR
08-29-2009, 08:26 AM
Check http://www.php.net/manual/en/class.simplexmlelement.php
Read about the xpath methods as well, once you learn how to form the queries, it is pretty easy to work with.
Here is a small example.
<?php
$xml = <<<EOD
<?xml version="1.0"?>
<book>
<title>My Book</title>
<chapter id="1">
<title>Chapter 1</title>
<para>Donec velit. Nullam eget tellus vitae tortor gravida scelerisque.
In orci lorem, cursus imperdiet, ultricies non, hendrerit et, orci.
Nulla facilisi. Nullam velit nisl, laoreet id, condimentum ut,
ultricies id, mauris.</para>
</chapter>
<chapter id="2">
<title>Chapter 2</title>
<para>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Proin
gravida. Phasellus tincidunt massa vel urna. Proin adipiscing quam
vitae odio. Sed dictum. Ut tincidunt lorem ac lorem. Duis eros
tellus, pharetra id, faucibus eu, dapibus dictum, odio.</para>
</chapter>
</book>
EOD;
$simplexml = new SimpleXMLElement($xml);
$title = $simplexml->title;
$chapter1Title = $simplexml->chapter[0]->title;
$chapter2Para = $simplexml->chapter[1]->para;
echo $title . '<br />' . $chapter1Title . '<br /> ' . $chapter2Para;
Outputs:
My Book
Chapter 1
Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Proin gravida. Phasellus tincidunt massa vel urna. Proin adipiscing quam vitae odio. Sed dictum. Ut tincidunt lorem ac lorem. Duis eros tellus, pharetra id, faucibus eu, dapibus dictum, odio.
teajay99
09-02-2009, 04:30 PM
Thanks Jason - that looks exactly what i need :)
I think i can get this working with what i have at the moment - i just need to extract the xml tags which exists as variable already. Hopefully the code below will make sense in what i am trying to do (if its possible!).
<?php
$xmlresult = "<book>
<title>My Book</title>
<chapter id="1">
<title>Chapter 1</title>
<para>Donec velit. Nullam eget tellus vitae tortor gravida scelerisque.
In orci lorem, cursus imperdiet, ultricies non, hendrerit et, orci.
Nulla facilisi. Nullam velit nisl, laoreet id, condimentum ut,
ultricies id, mauris.</para>
</chapter>
<chapter id="2">
<title>Chapter 2</title>
<para>Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Proin
gravida. Phasellus tincidunt massa vel urna. Proin adipiscing quam
vitae odio. Sed dictum. Ut tincidunt lorem ac lorem. Duis eros
tellus, pharetra id, faucibus eu, dapibus dictum, odio.</para>
</chapter>
</book>"
$xml = <<<EOD;
$xml.= $xmlresult;
$xml.= EOD;
$simplexml = new SimpleXMLElement($xml);
$title = $simplexml->title;
$chapter1Title = $simplexml->chapter[0]->title;
$chapter2Para = $simplexml->chapter[1]->para;
echo $title . '<br />' . $chapter1Title . '<br /> ' . $chapter2Para;
?>
Cheers
JShor
09-02-2009, 04:46 PM
Mark the thread as Resolved. Glad things worked out :)
teajay99
09-03-2009, 08:56 AM
Sorry - should have made it a bit more obvious that the code in my post above does not work and any advice would be much appreciated, only thing I have changed is the xml tags need to be declared as a variable.
Thanks,
Trace
JasonDFR
09-06-2009, 07:11 AM
You've got to get a handle on the basics. I would suggest going through some of the tutorials listed here.
http://www.google.com/webhp?hl=en#hl=en&q=simplexml+tutorial
Powered by vBulletin® Version 4.2.2 Copyright © 2021 vBulletin Solutions, Inc. All rights reserved.