当使用XML解析时,是您访问的对象的XML资源


when using XML parse, is the xml resource the object you access?

我似乎不太明白xml_parse是如何工作的。我所做的是获取xml文件的内容,创建一个解析器,并将其传递到xml_parse()中。我不知道以后该怎么办。我在想,在我的例子中,$xml是我现在可以迭代的数组。我试着看看如何解析我的数据。

    $fp = file_get_contents("memberdata.xml");
echo "pre-create";
$xml = xml_parser_create();
echo "pre-parse";
$status = xml_parse($xml,$fp);
if(!$status){
    die("Error parsing data from $fp into $xml");
}
echo "pre XML posting";
echo "<br />".$xml."<br />";
print_r($xml);
xml_parser_free($xml);

我似乎不知道如何访问这个。

示例xml数据如下:

<currentTime>2012-09-05 03:43:25</currentTime>
<result>
  <rowset name="members" key="characterID" columns="characterID,name,startDateTime,baseID,base,title,logonDateTime,logoffDateTime,locationID,location,shipTypeID,shipType,roles,grantableRoles">
    <row ..>
  </rowset>
</result>
<cachedUntil></cashedUntil>

SimpleXML将减轻很多挫折,而不是使用基本工具,而是使用其中一个工具包。这个问题的答案是使用SimpleXML重做的。

    $fp = file_get_contents("memberdata.xml");
$eve = new SimpleXMLElement($fp);
$cols = $eve->{'result'}->rowset['columns'];
//I put result in {} because result is a special character in php.
$cols = explode(",",$cols);
foreach ($cols as $c){
    echo "".$c."<br />";
}
    //output is printed to screen