PHP Retrieve DATA from XML

后端 未结 4 474
盖世英雄少女心
盖世英雄少女心 2021-01-24 06:13

My first attempt at retrieving data from XML for a maps application has failed. Here is a piece of the XML Feed.

         


        
相关标签:
4条回答
  • 2021-01-24 06:28
    function XMLReader()
    {
        $MyArray = array();
    
        $doc = new DOMDocument(); 
        $doc->load( 'XMLFilePath.xml' ); 
        $info = $doc->getElementsByTagName( "leg" );
        foreach( $info as $Type )
        {
            $details = $Type->getElementsByTagName( "start_address" );
            $detail = $details->item(0)->nodeValue;     
            $MyArray [] = $detail; 
        }
    
        return $MyArray;
    }
    

    and same for end_Address. I wish this answer is helpful.

    0 讨论(0)
  • 2021-01-24 06:39

    Haven't tested your specific case, but i remember running into something similar when using SimpleXML, you might want to use (string) to cast it out of the object

    array('output'=> (string)$start[0])
    

    Or rather just leave out $start = array($start) and just do

    array('output'=> (string)$start)
    

    On reading the SimpleXML XPath documentation (http://www.php.net/manual/en/simplexmlelement.xpath.php) again i think your problem might be this:

    Returns an array of SimpleXMLElement objects or FALSE in case of an error.
    

    So the XPath returns an array, then you wrap that in an array and take the first element of that array, so all you end up with is the original array - remove the array wrap and you should be fine

    0 讨论(0)
  • 2021-01-24 06:40

    Just echo $start? Also, why do you make an array of start and then output the first element, it doesnt make any sense at all.

    0 讨论(0)
  • 2021-01-24 06:50
    echo $xml->route->leg->start_address;
    

    This is the edited answer. I checked it. Its working properly on the XML.

    0 讨论(0)
提交回复
热议问题