![](/img/trans.png)
[英]Get all children from certain xml child element using SimpleXMLElement and xpath
[英]XML child nodes in xPath SimpleXMLElement
我在使用xpath時仍無法獲取子節點。 以前在某些情況下,我使用->children();
這確實對我有所幫助,但是在xpath上我無法使用children();。 我還嘗試使用//
來查看是否可以獲取所有子節點的內容,但仍然沒有運氣。
我的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);
另外,如何僅打印輸出? 我嘗試為每個循環使用echo,但是沒有用。
當我使用print_r時,它顯示為Array ( [0] => Array ( [Pubref] => [] ) )
我的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>
只需去除包圍<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());
此打印
object(SimpleXMLElement)#3 (1) {
["Pubref"]=>
object(SimpleXMLElement)#5 (3) {
["Year"]=>
string(4) "1965"
["Series"]=>
string(2) "VR"
["Pages"]=>
string(3) "204"
}
}
要顯示結果,只需執行另一個->children()
即可獲得所需的內容並使用foreach:
$array = $xml->xpath("/Notes/Note[@id=\"f2\"]/P");
$children = $array[0]->children()->children();
foreach($children as $k => $v)
echo "[$k] => $v";
編輯:如@ nl-x所建議,為了保持括號,您可以簡單地通過以下方式更新xpath: "/Notes/Note[@id=\\"f2\\"]/P/*"
。 然后在->children()
您將擁有所需的所有節點
您不能僅在xpath附帶的xmlelement列表上使用json_encode
。 xmlelement列表是xmlelements的數組。 json_encode強制xml元素執行->__toString()
,僅直接在元素內部返回文本。 (而不是子元素。)
因此,在您的示例中,代碼將返回.
帶有一些空格,因為您的xpath返回一個僅包含1個元素( <p>
)的xmlelement列表,並且該列表只有子元素和一些文本(空格和.
)。
要獲取元素的內部xml,應使用->asXML()
:
$xml = simplexml_load_string($xmlstring);
$elements = $xml->xpath('//Note[@id="f2"]/P');
foreach ($elements as $element) {
echo $element->asXML();
}
打印:
<P><Pubref>[<Year>1965</Year>] <Series>VR</Series> <Pages>204</Pages></Pubref>.</P>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.