如何获取xml子参数?
如何获取category
的所有结果并返回结果$category
.. category = Test1, Test2
我的xml
<xml>
<title>Test 123</title>
<categories>
<category>Test1</category>
<category>Test2</category>
</categories>
</xml>
我得到代码
$category = htmlspecialchars($item->categories->category, ENT_XML1 | ENT_QUOTES, 'UTF-8');
echo $category; //I want to return Test1, Test2
答案 0 :(得分:0)
您要搜索的是什么?
$xml = new SimpleXMLElement('<item id="1234">
<property name="country_id">
<value>4402</value>
</property>
<property name="rc_maintenance_other">
</property>
<property name="claim_right_shareholder">
</property>
<property name="charges_other">
</property>
<property name="other_expenses_heating">
</property>
<property name="unpaid_bills_amount">
</property>
<property name="iv_person_phone">
<value>03-6756711</value>
</property>
</item>
');
foreach ($xml->xpath('//item[@id="1234"]') as $item)
{
foreach ($item->children() as $child) {
echo $child['name'] ."\n";
}
}
重复的问题? Access all children of a certain node with simplexml and php
答案 1 :(得分:0)
您可以使用$item->categories->category
循环foreach
:
$source = <<<DATA
<xml>
<title>Test 123</title>
<categories>
<category>Test1</category>
<category>Test2</category>
</categories>
</xml>
DATA;
$item = simplexml_load_string($source);
foreach ($item->categories->category as $elm) {
echo $elm . PHP_EOL;
}
这将为您提供:
Test1
Test2
查看php demo