1

大きなxmlファイルから( xmlstarlet http://xmlstar.sourceforge.net/を使用して)ノードを抽出し、このノードをphp配列に解析するソリューションを探しています。

elements.xml

<?xml version="1.0"?>
<elements>
  <element id="1" par1="val1_1" par2="val1_2" par3="val1_3">
    <title>element 1 title</title>
    <description>element 1 description</description>
  </element>
  <element id="2" par1="val2_1" par2="val2_2" par3="val2_3">
    <title>element 2 title</title>
    <description>element 2 description</description>
  </element>
</elements>

xmlstarletを使用してid="1"の要素タグを抽出するには、このシェルコマンドを実行しています...

xmlstarlet sel -t -c "/elements/element[id=1]" elements.xml

このシェルコマンドは次のようなものを出力します...

<element id="1" par1="val1_1" par2="val1_2" par3="val1_3">
  <title>element 1 title</title>
  <description>element 1 description</description>
</element>

このシェル出力をphp配列に解析するにはどうすればよいですか?

ありがとうございました。


http://php.net/manual/en/function.simplexml-load-string.phpは非常に便利ですこのSimpleXML関数は、抽出されたXMLをSimpleXMLオブジェクトに変換します。

4

1 に答える 1

1

いつでも新しく出力された xml を取得して、次のように simplexml を使用できます。

$data = '<element id="1" par1="val1_1" par2="val1_2" par3="val1_3">
<title>element 1 title</title>
<description>element 1 description</description>
</element>';


$xml = simplexml_load_string($data);
echo $xml->title; //access title
echo $element_ID = $xml->attributes()->id; //access elements attributes by name

そして今$xml、あなたが必要とする配列です

于 2010-04-18T12:50:43.367 に答える