简体   繁体   English

xPath SimpleXMLElement中的XML子节点

[英]XML child nodes in xPath SimpleXMLElement

from this answer XML data extraction using PHP simpleXML 从这个答案使用PHP simpleXML提取XML数据

I am still having trouble fetching child nodes while using xpath. 我在使用xpath时仍无法获取子节点。 Previously in some context I used ->children(); 以前在某些情况下,我使用->children(); which did help me but while on xpath I am unable to use children(); 这确实对我有所帮助,但是在xpath上我无法使用children();。 I also tried using // to see if that can fetch all the child nodes content but still no luck. 我还尝试使用//来查看是否可以获取所有子节点的内容,但仍然没有运气。

My PHP code is 我的PHP代码是

$xml = simplexml_load_string(file_get_contents($fileloc));
$json = json_encode($xml);
$array = json_decode($json,TRUE);
$node = $xml->xpath('//Note[@id="f2"]/P');
$array = json_decode(json_encode($node), true);
print_r($array);

Also, how can print just the output? 另外,如何仅打印输出? I tried using for each loop with echo but it didn't work. 我尝试为每个循环使用echo,但是没有用。

when I use print_r its displaying as Array ( [0] => Array ( [Pubref] => [] ) ) 当我使用print_r时,它显示为Array ( [0] => Array ( [Pubref] => [] ) )

My XML structure is like 我的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>

Just strip the square brackets that surrounds the <year> child and it works to me: 只需去除包围<year>孩子的方括号,它对我有用:

$xml = new SimpleXMLElement(
'<Notes>
    <Note id="f1" type="foot" display="1">
        <P>The second respondent by virtue of the extended definition of [...] 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>
    [....]
</Notes>');

$array = $xml->xpath("/Notes/Note[@id=\"f2\"]/P");
var_dump($array[0]->children());

This prints 此打印

object(SimpleXMLElement)#3 (1) {
  ["Pubref"]=>
  object(SimpleXMLElement)#5 (3) {
    ["Year"]=>
    string(4) "1965"
    ["Series"]=>
    string(2) "VR"
    ["Pages"]=>
    string(3) "204"
  }
}

To show results, just do another ->children() to get the ones you need and use a foreach: 要显示结果,只需执行另一个->children()即可获得所需的内容并使用foreach:

$array = $xml->xpath("/Notes/Note[@id=\"f2\"]/P");
$children = $array[0]->children()->children();

foreach($children as $k => $v)
    echo "[$k] => $v";

EDIT: As suggested by @nl-x, in order to keep the brackets you can simply update your xpath this way: "/Notes/Note[@id=\\"f2\\"]/P/*" . 编辑:如@ nl-x所建议,为了保持括号,您可以简单地通过以下方式更新xpath: "/Notes/Note[@id=\\"f2\\"]/P/*" Then in ->children() you'll have all the nodes you need 然后在->children()您将拥有所需的所有节点

You cannot just use json_encode on the xmlelement list that you get with your xpath. 您不能仅在xpath附带的xmlelement列表上使用json_encode The xmlelement list is an array of xmlelements. xmlelement列表是xmlelements的数组。 And json_encode forces the xml elements to do a ->__toString() , returning only texts directly inside the element. json_encode强制xml元素执行->__toString() ,仅直接在元素内部返回文本。 (And not of the child elements.) (而不是子元素。)

So in your example the code will return a . 因此,在您的示例中,代码将返回. with some whitespaces, as your xpath returns an xmlelement list with just 1 element (the <p> ), and that one has only child elements and some text (whitespaces and a . ) 带有一些空格,因为您的xpath返回一个仅包含1个元素( <p> )的xmlelement列表,并且该列表只有子元素和一些文本(空格和. )。

To get the inner xml of the elements, you should use ->asXML() : 要获取元素的内部xml,应使用->asXML()

$xml = simplexml_load_string($xmlstring);
$elements = $xml->xpath('//Note[@id="f2"]/P');
foreach ($elements as $element) {
    echo $element->asXML();
}

This prints: 打印:

<P><Pubref>[<Year>1965</Year>] <Series>VR</Series> <Pages>204</Pages></Pubref>.</P>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM