使用PHP simpleXML提取XML数据


XML data extraction using PHP simpleXML

我正在尝试从XML文件中检索XML数据,当我使用children()时,我希望它会检索子节点的数据,但它不会检索。在以下XML文件中

<Notes>
    <Note id="f1" type="foot" display="1">
        <P>The second respondent by virtue of the extended definition of “foreign state” in s 3(3) of the Act.</P>
    </Note>
    <Note id="f2" type="foot" display="2">
        <P>
            <Pubref>[<Year>1965</Year>] <Series>VR</Series><Pages>204</Pages></Pubref>.</P>
    </Note>
    <Note id="f3" type="foot" display="3">
        <P>Ibid, 206.</P>
    </Note>
    <Note id="f4" type="foot" display="4">
        <P>
            <Emph type="i">Foreign State Immunity</Emph>, ALRC 24, (1984).</P>
    </Note>
    <Note id="f5" type="foot" display="5">
        <P>Ibid, 55, [93].</P>
    </Note>
    <Note id="f6" type="foot" display="6">
        <P>Macquarie Dictionary (5th ed, 2009), 1475.</P>
    </Note>
    <Note id="f7" type="foot" display="7">
        <P>Ibid, 727.</P>
    </Note>
    <Note id="f8" type="foot" display="8">
        <P>
            <Emph type="i">Foreign State Immunity</Emph>, ALRC 24, 1984, p xviii, [17] and p 51, [90].</P>
    </Note>
    <Note id="f9" type="foot" display="9">
        <P>
            <Pubref>(<Year>2011</Year>) <Vol>192</Vol><Series>FCR</Series><Pages>393</Pages></Pubref>.</P>
    </Note>
    <Note id="f10" type="foot" display="10">
        <P>Ibid, 437.</P>
    </Note>
    <Note id="f11" type="foot" display="11">
        <P>
            <Pubref>(<Year>2004</Year>) <Vol>185</Vol><Series>FLR</Series><Pages>48</Pages></Pubref>.</P>
    </Note>
    <Note id="f12" type="foot" display="12">
        <P>Ibid, [108].</P>
    </Note>
    <Note id="f13" type="foot" display="13">
        <P>(2005) 12 VR 340, 346.</P>
    </Note>
    <Note id="f14" type="foot" display="14">
        <P>
            <Pubref>[<Year>1987</Year>] <Vol>1</Vol><Series>Qd R</Series><Pages>221</Pages></Pubref>.</P>
    </Note>
    <Note id="f15" type="foot" display="15">
        <P>
            <MNCit>[2008] QCA 328</MNCit>.</P>
    </Note>
    <Note id="f16" type="foot" display="16">
        <P>
            <Pubref>[<Year>2013</Year>] <Vol>1</Vol><Series>Qd R</Series><Pages>204</Pages></Pubref>.</P>
    </Note>
    <Note id="f17" type="foot" display="17">
        <P>[1955] AC 72.</P>
    </Note>
    <Note id="f18" type="foot" display="18">
        <P>
            <Pubref>(<Year>1988</Year>) <Vol>17</Vol><Series>NSWLR</Series><Pages>623</Pages></Pubref>.</P>
    </Note>
    <Note id="f19" type="foot" display="19">
        <P>
            <Pubref>(<Year>2010</Year>) <Vol>79</Vol><Series>NSWLR</Series><Pages>513</Pages></Pubref>.</P>
    </Note>
    <Note id="f20" type="foot" display="20">
        <P>[1955] AC 72, 87.</P>
    </Note>
    <Note id="f21" type="foot" display="21">
        <P>Ibid, 89–90.</P>
    </Note>
    <Note id="f22" type="foot" display="22">
        <P>
            <Pubref>(<Year>1988</Year>) <Vol>17</Vol><Series>NSWLR</Series><Pages>623</Pages></Pubref>, 630.</P>
    </Note>
    <Note id="f23" type="foot" display="23">
        <P>
            <Pubref>(<Year>2010</Year>) <Vol>79</Vol><Series>NSWLR</Series><Pages>513</Pages></Pubref>.</P>
    </Note>
    <Note id="f24" type="foot" display="24">
        <P>
            <Pubref>(<Year>1988</Year>) <Vol>17</Vol><Series>NSWLR</Series><Pages>623</Pages></Pubref>, 633 per Cole J cited in <Xref ref="cr11" role="cr"><Name><Emph type="i">Zhang v Zemin</Emph></Name><Pubref>(<Year>2010</Year>) <Vol>79</Vol><Series>NSWLR</Series><Pages>513</Pages></Pubref>, 524</Xref>.</P>
    </Note>
    <Note id="f25" type="foot" display="25">
        <P>
            <Pubref>[<Year>1983</Year>] <Vol>1</Vol><Series>AC</Series><Pages>244</Pages></Pubref>.</P>
    </Note>
    <Note id="f26" type="foot" display="26">
        <P>
            <Pubref>(<Year>2012</Year>) <Vol>247</Vol><Series>CLR</Series><Pages>240</Pages></Pubref>.</P>
    </Note>
    <Note id="f27" type="foot" display="27">
        <P>
            <Pubref>[<Year>1983</Year>] <Vol>1</Vol><Series>AC</Series><Pages>244</Pages></Pubref>, 262.</P>
    </Note>
    <Note id="f28" type="foot" display="28">
        <P>At [23].</P>
    </Note>
</Notes>

我使用了以下PHP代码来打印这个数据

$fileloc = "../qjfiles/c2014-2.001.xml";
$filedata = new SimpleXMLElement(file_get_contents($fileloc));
foreach ($filedata->Notes->children() as $filexmldata) {
    echo $filexmldata->children()."<br/>";
}

输出显示如下

The second respondent by virtue of the extended definition of “foreign state†in s 3(3) of the Act.
.
Ibid, 206.
, ALRC 24, (1984).
Ibid, 55, [93].
Macquarie Dictionary (5th ed, 2009), 1475.
Ibid, 727.
, ALRC 24, 1984, p xviii, [17] and p 51, [90].
.
Ibid, 437.
.
Ibid, [108].
(2005) 12 VR 340, 346.
.
.
.
[1955] AC 72.
.
.
[1955] AC 72, 87.
Ibid, 89–90.
, 630.
.
, 633 per Cole J cited in .
.
.
, 262.
At [23].

如何分别显示<Note id="f1"<Note id="f2"的输出?

我试过像$filexmldata->Note[1]->children()一样,但它不起作用

这个问题的答案

How can I display the output of <Note id="f1" separately and <Note id="f2" separately?

您可以使用xpath方法。下面是一个关于如何获取包含id="f2"的节点的示例:

$xml = simplexml_load_string($xml);
$node = $xml->xpath('/Notes/Note[@id="f2"]');

您可以替换下面的php代码并尝试吗

 <?php
$fileloc = "../qjfiles/c2014-2.001.xml";
$filedata = new SimpleXMLElement(file_get_contents($fileloc));
    foreach ($filedata as $filexmldata) {
        foreach($filexmldata->attributes() as $a=> $b) {
             echo $a,'="',$b,"'"'n";
        }
        echo '<pre>'; print_r($filexmldata->P);
    }
?>