SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    May 2006
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Parsing with SimpleXML & PHP

    Hi Guys,

    I am pretty new to SimpleXML and need to edit the following..

    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; 
    ?>
    Problem i am having is the XML tags are stored in an existing variable returned from a third party server so need something similar to this (but working!!)..

    Code:
    $xml.= "<<<EOD";
    $xml.= "$xml_result";
    $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;
    Any advice would be much appreciated

    Thanks in advance.

    Trace

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Use $xml_result directly, don't add anything to it. The "EOD" is heredoc syntax, not part of the text in your first code block.

  3. #3
    SitePoint Member
    Join Date
    May 2006
    Posts
    4
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks Dan,

    I had tried this originally but i must be missing something. Below is part of the code. (with URL removed) The commented part is my addition.

    Code:
    $xml = "<Inputs>";
    $xml.= "<Authentication>";
    $xml.= "<Username>$username</Username>";
    $xml.= "<Password>$password</Password>";
    $xml.= "<RequestType>Term</RequestType>";
    $xml.= "<RequestFrom>$system_name</RequestFrom>";
    $xml.= "<RequestFromCode>$system_code</RequestFromCode>";
    $xml.= "</Authentication>";
    
    
    $xml.= "<Life1>";
    $xml.= "<DOB>$dob</DOB>";
    $xml.= "<Sex>$sex</Sex>";
    $xml.= "<Smoker>$smoker</Smoker>";
    $xml.= "<LifeCover>$lifecover</LifeCover>";
    $xml.= "<IllnessCover>N</IllnessCover>";
    $xml.= "</Life1>";
    
    $xml.= "<Life2>";
    $xml.= "</Life2>";
    
    $xml.= "<Plan>";
    $xml.= "<Term>$term</Term>";
    $xml.= "<Indexation>N</Indexation>";
    $xml.= "<MortgageInterest>6</MortgageInterest>";
    $xml.= "<Frequency>Monthly</Frequency>";
    $xml.= "<QuoteLifeOnly>Y</QuoteLifeOnly>";
    $xml.= "<QuoteLifeAccelerated>$accelerated</QuoteLifeAccelerated>";
    $xml.= "<QuoteLifeIllness>N</QuoteLifeIllness>";
    $xml.= "<QuoteIllnessOnly>N</QuoteIllnessOnly>";
    $xml.= "</Plan>";
    $xml.= "</Inputs>";
    
    
    $url = 'URL NORMALLY HERE';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_POST, 1);
    curl_setopt($ch, CURLOPT_POSTFIELDS, "xml=".urlencode($xml));
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    $xml_result=curl_exec($ch);
    curl_close($ch);
    
    print("[$xml_result]");
    
    /* simple xml */
    
    $simplexml = new SimpleXMLElement($xml_result);
    
    $Name = $simplexml->Name;
    
    echo $Name; 
    
    /* end */
    
    $data = XML_unserialize($xml_result);
    $xml_result contains... (with usernames/passwords removed)

    Code:
    <Result>
    <Errors></Errors>
    <Inputs><Authentication><Username>####</Username>
    <Password>####</Password><RequestType>Term</RequestType><RequestFrom>####</RequestFrom><RequestFromCode>####</RequestFromCode></Authentication><Life1><DOB>01/01/1969</DOB><Sex>Male</Sex><Smoker>Non-Smoker</Smoker><LifeCover>100,000</LifeCover><IllnessCover>N</IllnessCover></Life1><Life2></Life2><Plan><Term>10</Term><Indexation>N</Indexation><MortgageInterest>6</MortgageInterest><Frequency>Monthly</Frequency><QuoteLifeOnly>Y</QuoteLifeOnly><QuoteLifeAccelerated>Non-Smoker</QuoteLifeAccelerated><QuoteLifeIllness>N</QuoteLifeIllness><QuoteIllnessOnly>N</QuoteIllnessOnly></Plan></Inputs><Outputs>
    <Summary>
    <Life1>Male, 40 years 8 months (01/01/1969), Non-Smoker, life cover 100,000</Life1>
    <Life2></Life2>
    <Plan>10 year term, mortgage interest 6.00%, benefits and premiums not increasing.<p>1% levy included.</p></Plan>
    </Summary>
    <Quotes>
    <Type>
    <Desc>Life Cover Only</Desc>
    <Company>
    <Name key="1">Caledonian Life</Name>
    <PricePledge>False</PricePledge>
    <Underwriting1></Underwriting1>
    <SLevel key="2">14.50</SLevel>
    <SConvertible key="2">15.46</SConvertible>
    <SMortgage key="2">12.12</SMortgage>
    </Company>
    <Company>
    <Name key="2">Canada Life</Name>
    <PricePledge>False</PricePledge>
    <Underwriting1></Underwriting1>
    <SLevel key="2">15.15</SLevel>
    <SConvertible key="2">15.91</SConvertible>
    <SMortgage key="2">13.13</SMortgage>
    </Company>
    </Type>
    </Quotes>
    </Outputs>
    </Result>
    Cheers,

    Trace


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
  •