关闭

php通过xpath解析xml数据

164人阅读 评论(0) 收藏 举报
分类:
<?php
$xmlStr = '
<infos>
<para name="name"><note>note1</note><extra>extra1</extra></para>
<para></para>
<para><note>note2</note></para>
<para><note><para></para></note><extra>extra3</extra></para>
</infos>';
// xpath filepath
$xmlDoc = new  DOMDocument();
$xmlDoc->loadXML($xmlStr);
$xpath = new DOMXPath($xmlDoc); 
$result = $xpath->query('/infos/para');
//使用下标遍历
for ($i = 0;$i < $result->length;$i++) {
	# code...
}
//使用遍历器遍历
foreach ($result as $item) {//如果要使循环内对$item的处理影响到$result的值,可使用&$item替换$item
	//print_r($item);//结果1
	// foreach ($item->attributes as $childitem) {//遍历属性
		// print_r($childitem);//结果2
	// }
	if($item->attributes->length > 0){
			print_r($item->attributes->getNamedItem('name')->name);//结果3
	}
}




//结果1
// DOMElement Object
// (
//     [tagName] => para
//     [schemaTypeInfo] => 
//     [nodeName] => para
//     [nodeValue] => note1extra1
//     [nodeType] => 1
//     [parentNode] => (object value omitted)
//     [childNodes] => (object value omitted)
//     [firstChild] => (object value omitted)
//     [lastChild] => (object value omitted)
//     [previousSibling] => (object value omitted)
//     [nextSibling] => (object value omitted)
//     [attributes] => (object value omitted)
//     [ownerDocument] => (object value omitted)
//     [namespaceURI] => 
//     [prefix] => 
//     [localName] => para
//     [baseURI] => file:///D:/home/guoqiang_test/a2m-portal/
//     [textContent] => note1extra1
// )
// DOMElement Object
// (
//     [tagName] => para
//     [schemaTypeInfo] => 
//     [nodeName] => para
//     [nodeValue] => 
//     [nodeType] => 1
//     [parentNode] => (object value omitted)
//     [childNodes] => (object value omitted)
//     [firstChild] => 
//     [lastChild] => 
//     [previousSibling] => (object value omitted)
//     [nextSibling] => (object value omitted)
//     [attributes] => (object value omitted)
//     [ownerDocument] => (object value omitted)
//     [namespaceURI] => 
//     [prefix] => 
//     [localName] => para
//     [baseURI] => file:///D:/home/guoqiang_test/a2m-portal/
//     [textContent] => 
// )
// DOMElement Object
// (
//     [tagName] => para
//     [schemaTypeInfo] => 
//     [nodeName] => para
//     [nodeValue] => note2
//     [nodeType] => 1
//     [parentNode] => (object value omitted)
//     [childNodes] => (object value omitted)
//     [firstChild] => (object value omitted)
//     [lastChild] => (object value omitted)
//     [previousSibling] => (object value omitted)
//     [nextSibling] => (object value omitted)
//     [attributes] => (object value omitted)
//     [ownerDocument] => (object value omitted)
//     [namespaceURI] => 
//     [prefix] => 
//     [localName] => para
//     [baseURI] => file:///D:/home/guoqiang_test/a2m-portal/
//     [textContent] => note2
// )
// DOMElement Object
// (
//     [tagName] => para
//     [schemaTypeInfo] => 
//     [nodeName] => para
//     [nodeValue] => extra3
//     [nodeType] => 1
//     [parentNode] => (object value omitted)
//     [childNodes] => (object value omitted)
//     [firstChild] => (object value omitted)
//     [lastChild] => (object value omitted)
//     [previousSibling] => (object value omitted)
//     [nextSibling] => (object value omitted)
//     [attributes] => (object value omitted)
//     [ownerDocument] => (object value omitted)
//     [namespaceURI] => 
//     [prefix] => 
//     [localName] => para
//     [baseURI] => file:///D:/home/guoqiang_test/a2m-portal/
//     [textContent] => extra3
// )
// 结果2
// (
//     [name] => name
//     [specified] => 1
//     [value] => name
//     [ownerElement] => (object value omitted)
//     [schemaTypeInfo] => 
//     [nodeName] => name
//     [nodeValue] => name
//     [nodeType] => 2
//     [parentNode] => (object value omitted)
//     [childNodes] => (object value omitted)
//     [firstChild] => (object value omitted)
//     [lastChild] => (object value omitted)
//     [previousSibling] => 
//     [attributes] => 
//     [ownerDocument] => (object value omitted)
//     [namespaceURI] => 
//     [prefix] => 
//     [localName] => name
//     [baseURI] => file:///D:/home/guoqiang_test/a2m-portal/
//     [textContent] => name
// )
//结果3
//name

XML Elemnt元素节点类型为 DOMElement 类型,在这里他的集合(如childNodes,只包含下一层的Element)类型为DOMNodeList,DOMNodeList可通过遍历器便利,或通过下标使用item(index)方法遍历(index为你指定的下标ID);Elment元素的值为nodeValue,它为其包含类的所有文本节点的结合如

<para><note><para>para</para></note><extra>extra3</extra></para>

最外层para Element节点的nodeValue值为paraextra3;


XML Attr属性节点类型为 DOMAttr 类型,在这里他的集合(如attributes)类型为DOMNamedNodeMap,DOMNamedNodeMap可通过遍历器遍历,或通过下标使用item(index)方法遍历(index为你指定的下标ID),也可通过属性名使用getNamedItem(Name)获取指定Name的属性

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:17319次
    • 积分:383
    • 等级:
    • 排名:千里之外
    • 原创:19篇
    • 转载:19篇
    • 译文:0篇
    • 评论:0条
    文章分类