Advanced Search

Results 1 to 6 of 6

Thread: Parsing XML with PHP

  1. #1
    Join Date
    Aug 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Parsing XML with PHP

    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-a...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

  2. #2
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    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 Code:
    <?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.
    Last edited by JasonDFR; 08-29-2009 at 08:32 AM.

  3. #3
    Join Date
    Aug 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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!).

    Code:
    <?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

  4. #4
    Join Date
    Mar 2007
    Location
    New York, NY
    Posts
    557
    Thanks
    8
    Thanked 66 Times in 66 Posts

    Default

    Mark the thread as Resolved. Glad things worked out
    - Josh

  5. #5
    Join Date
    Aug 2009
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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
    Last edited by teajay99; 09-03-2009 at 09:10 AM.

  6. #6
    Join Date
    Apr 2008
    Location
    Limoges, France
    Posts
    395
    Thanks
    13
    Thanked 61 Times in 61 Posts

    Default

    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...lexml+tutorial

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •