开发者

xml that won't extract

I use simplexml_load_file to get the following xml conten开发者_如何转开发t into a variable

<suggestions> 
    <items> 

        <item stop_id="00018210" stop_id_with_hash_key="00018210!-1579576589" shortcut=".lvc" stop_type="Stop" rt90_x="6401305" rt90_y="1285996"> 
        <friendly_name><![CDATA[Landvetter centrum, HÄRRYDA (Hållplats)]]></friendly_name> 
        <stop_name><![CDATA[Landvetter centrum]]></stop_name> 
        <county><![CDATA[HÄRRYDA]]></county> 
        </item> 

    </items> 
</suggestions> 

I try to extract the content by doing the following with php

foreach ($xml->suggestions as $suggestion) { 
    foreach ($suggestion->items as $item) { 
        foreach ($item->friendly_name as $name) { 
            echo $name; 
        } 
    } 
} 

But I get nothing. I also try to put it through the standard parser, but end up with an empty result. Is there anything in the xml data above that requires me to handle this one differently?


Your file is missing the xml header, add this to the beginning:

<?xml version="1.0" encoding="utf-8"?>


Try this

    foreach ($xml->items as $item) {
        foreach($item as $item2) {
          echo $item2->friendly_name;
        }
    } 

The suggestions is marking the start/end ( root element ).


You are iterating too much. You can basically access your element as:

print $xml->items[0]->item->friendly_name;

You don't need to loop over the root element. There will only be one, and it will be contained in the main $xml (sans node name). The only element you need to loop over in your case is <items>. Then you can access <item> with the ->friendly_name as direct properties:

foreach ($xml->items as $items) {
    print $items->item->friendly_name;
}


Thank you for your suggestions.

Maerlyn: The document has that, I just left it out including only the structure.

Maria & Daniq, I don't know if i understood you correctly, but this is what I did, and none of your approaches yielded any results:

$xml = simplexml_load_file("http://www.vasttrafik.se/External_Services/TravelPlanner.asmx/GetStopsSuggestions?identifier=0&searchString=landvetter&count=10");

    foreach ($xml->items as $item) {
        foreach($item as $item2) {
          echo $item2->friendly_name;
        }
    } 

So that didn't get me anything, neither does this:

$xml = simplexml_load_file("http://www.vasttrafik.se/External_Services/TravelPlanner.asmx/GetStopsSuggestions?identifier=0&searchString=landvetter&count=10");

foreach ($xml->items as $items) {
    echo $items->item->friendly_name;
}

If any of you have the time, feel free to load this xml document yourself and see if you can get anything out of it... much appreciated. I am an xml novice, life is too short...

/Mattias

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜