找回密码
 加入
搜索
查看: 1872|回复: 0

[系统综合] 如何读取xml中带命名空间的节点元素

[复制链接]
发表于 2013-11-15 12:31:10 | 显示全部楼层 |阅读模式
使用下面的代码,如果xml文档中不包含命名空间的话,没有问题,可以取得正确的内容。
#include <Array.au3>
#include <ACN_MSXML.au3>

Dim $oXMLDoc, $strXPath, $sPath, $Element[1], $n = 0

_MSXML_InitInstance($oXMLDoc)
If @error Then
        MsgBox(0, "Error", "Failed to create instance")
        Exit
EndIf
_MSXML_FileOpen($oXMLDoc, @ScriptDir & "\test2.xml")
If @error Then
        MsgBox(0, "Error", _MSXML_Error())
Else
        $Element[0] = "/"
        While 1
                $n = $n + 1
                If $n - 1 > UBound($Element) - 1 Then ExitLoop
                Node($Element[$n - 1])
        WEnd
EndIf
_ArrayDisplay($Element, '所有元素路径')
$oXMLDoc = 0
Exit

Func Node($strXPath)
        $Nodes = _MSXML_GetChildNodes($oXMLDoc, $strXPath)
        If @error = 1 Then
        ElseIf $Nodes[0] = 0 Then
                $text = _MSXML_GetChildText($oXMLDoc, $strXPath)
                MsgBox(0, "元素", "元素路径:" & $strXPath & @CRLF & "元素文本内容:" & $text[1])
        Else
                For $i = 1 To $Nodes[0]
                        $nNodes = _MSXML_GetNodeCount($oXMLDoc, $strXPath & "/" & $Nodes[$i])
                        For $m = 1 To $nNodes
                                If $strXPath = "/" Then
                                        $sPath = $Nodes[$i]
                                Else
                                        $sPath = StringReplace($strXPath & "/" & $Nodes[$i] & "[" & $m & "]", "[1]", "")
                                EndIf
                                _ArraySearch($Element, $sPath)
                                If @error Then _ArrayAdd($Element, $sPath)
                        Next
                Next
        EndIf
EndFunc   ;==>Node
但是当xml文档中含有命名空间时,上面的代码就不能正确取得所需的值。
xml文档内容如下:
<m:list xmlns:m="http://www.xxx.com/xml">
        <l title="音乐">
                <m type="" src="test.mp3">mp3音频</m>
        </l>
        <l title="视频">
                <m type="" src="test.rmvb">rmvb视频</m>
        </l>
</m:list>
请教各位,如何能取得上面的节点元素呢。要处理的文档中,命名空间有好几个,有什么好的办法吗?谢谢。
您需要登录后才可以回帖 登录 | 加入

本版积分规则

QQ|手机版|小黑屋|AUTOIT CN ( 鲁ICP备19019924号-1 )谷歌 百度

GMT+8, 2024-5-4 03:50 , Processed in 0.076943 second(s), 23 queries .

Powered by Discuz! X3.5 Licensed

© 2001-2024 Discuz! Team.

快速回复 返回顶部 返回列表