我觉得问题重复了,但我对前面的答案不满意。
我有一个XML文件名为
<?xml version="1.0" encoding="UTF-8"?> <forms xml:id="cust_type">
<form>
<element id="cust_type" type="dropdown" name="cust_type" desc="Customer Type*" value="" div_attr="select_long"
class="required select" validate="required"></element>
<element type="text" name="form_no" desc="Form No" div_attr="txt_short" value="" class="number" validate="numeric"></element>
</form>
</forms>
我只想获得id="cust_type"或name="cust_type"的元素。但是我不明白。
请建议我使用SimpleXML或DOMDocument的代码。
使用simplexml
和xpath
:
$xml = simplexml_load_string($x); // assume XML in $x
$elements = $xml->xpath("//element[@id='cust_type' or @name='cust_type']");
foreach ($elements as $element) echo $element['name'] . "<br />";
参见工作演示:http://codepad.viper-7.com/EzmGWJ