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

时间:2012-09-05 04:19:29

标签: php xml xml-parsing

我似乎不太明白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>

1 个答案:

答案 0 :(得分:1)

使用其中一个工具包而不是使用基础工具,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