2011-10-19 11 views
0

私は以下のXMLファイルを持っています。 PHPを使用してこのXMLファイルを解析したいと思います。PHPで複数の類似したXMLタグを解析するには?

<id_list> 
    - 
    <ids> 
     <id>195002349</id> 
     <id>374487611</id> 
     <id>192983648</id> 
     <id>168378766</id>` 
     <id>161573001</id> 
    </ids> 

    <next_cursor>0</next_cursor> 
    <previous_cursor>0</previous_cursor> 
</id_list> 

私は形式で出力したい:(SimpleXMLElement($file)付き)XMLを読み込む場合element`を読むために "IDS" を検索し、whileループを行うためにXPathを使用し、

Id1=195002349 Id2=374487611 Id3=192983648 Id4=168378766 Id5=161573001 
+0

あなたも知っていますが、楽しむためにhttp://stackoverflow.com/questions/ask-adviceを読んではいけません。質問をする前に研究をしてください。 – Gordon

答えて

0

をテキスト。

ので、同じように:

$notes = new SimpleXMLElement('test2.xml', NULL, true); 
$all = array(); 
$results = $notes->xpath("/id_list/ids/id"); 
foreach($results as $to){ 
    echo "<br>".$to; 
    $all[]=(string)$to; 
} 

print_r($all); 
0

これは、テキストの指定された2つの部分の間の文字列を取り除くために便利な小さな機能です。これは、XMLテキスト、bbCode、またはその他の区切られたコード/テキストを解析するために使用できます。

function get_string_between($string, $start, $end){ 
    $string = " ".$string; 
    $ini = strpos($string,$start); 
    if ($ini == 0) return ""; 
    $ini += strlen($start); 
    $len = strpos($string,$end,$ini) - $ini; 
    return substr($string,$ini,$len); 
} 

$fullstring = "this is my [tag]dog[/tag]"; 
$parsed = get_string_between($fullstring, "[tag]", "[/tag]"); 

echo $parsed; // (result = dog) 
+0

なぜそれが便利で、OPの質問にどう答えますか? – Gordon

関連する問題