0

xml ファイルから特定のデータをフェッチするつもりです。以前simple_load_file()は xml ファイルを読み込んでオブジェクト要素を取得していましたが、それらにアクセスする方法がわかりません。xml ファイルは次のようになります。

<?mxl version="1.0">
<metaData>
<Application version="1.0" type="32">
   <options>
       <section name="A">
           <description>...</description>
           ...
       <section name="B">
       ....
   </options>
</Application>
</metaData>

私のコード:

$xml = simplexml_load_file($url);
echo $xml->Application->version; // get the version but failed
echo $xml->Application->options->section...//I want to get the data from each section, but I don't know how to visit the elements.
4

2 に答える 2

3

これを試して

// attribute accessing
$version = (string)$xml->Application['version']
// or
$version = (string)$xml->Application->attributes()->version;


// acess children
foreach($xml->Application->section as $section)
{
    // you can work with single section here
}

// or other way
foreach($xml->Application->children() as $section)
{
    // you can work with single section here
}
于 2013-10-10T20:43:45.143 に答える
0

この質問に答える前に、ちょっとしたヒントを教えてください。問題が発生したときはいつでも、この場合のように Google で検索してみてください。

PHP simplexml examples

さて、XML コンテンツがあるとしましょう:

<?php
$xmlstr = <<<XML
<?xml version='1.0' standalone='yes'?>
<movies>
 <movie>
  <title>PHP: Behind the Parser</title>
  <characters>
   <character>
    <name>Ms. Coder</name>
    <actor>Onlivia Actora</actor>
   </character>
   <character>
    <name>Mr. Coder</name>
    <actor>El Act&#211;r</actor>
   </character>
  </characters>
  <plot>
   So, this language. It's like, a programming language. Or is it a
   scripting language? All is revealed in this thrilling horror spoof
   of a documentary.
  </plot>
  <great-lines>
   <line>PHP solves all my web problems</line>
  </great-lines>
  <rating type="thumbs">7</rating>
  <rating type="stars">5</rating>
 </movie>
</movies>
XML;
?>

XML データは次のように解析できます。

<?php


$movies = new SimpleXMLElement($xmlstr);

echo $movies->movie[0]->plot;
?>

より多くの例については、http: //php.net/manual/en/simplexml.examples-basic.phpをご覧ください。

この質問固有の場合、SimpleXMLElement::childrenを使用する必要があります

于 2013-10-10T20:39:56.467 に答える