Visual Basic 使用 Xml 文档解析 Xml 文件

Visual Basic Parse Xml File Using XmlDocument

我有 xml 个包含以下代码的文件:

<?xml version='1.0' encoding='utf-8'?>
<widget id="PACKAGE_NAME" version="PROJECT_VERSION" xmlns="http://www.w3.org/ns/widgets" xmlns:gap="http://phonegap.com/ns/1.0">
    <name>PROJECT_NAME</name>
    <preference name="android-minSdkVersion" value="MIN_SDK" />
    <preference name="android-versionCode" value="VERSION_CODE" />
    <preference name="android-installLocation" value="INSTALL" />
    <preference name="android-targetSdkVersion" value="TARGET_SDK" />
    <preference name="orientation" value="ORIENTATION" />
    <preference name="fullscreen" value="FULLSCREEN" />
</widget>

我有这个变量:

Dim PackageName, ProjectName, ProjectVersion, VersionCode, MinSdk, TargetSdk, InstallLocation, Orientation, FullScreen As String

我使用此代码获取 ProjectName、PackageName 和 ProjectVersion:

Dim doc as XmlDocument = new XmlDocument()
doc.Load("config.xml")
Dim root As XmlElement = doc.DocumentElement
PackageName = root.GetAttribute("id")
ProjectVersion = root.GetAttribute("version")
ProjectName = root.GetElementsByTagName("name").Item(0).InnerText

但我无法获取此值:

VersionCode = VERSION_CODE
MinSdk = MIN_SDK
TargetSdk = TARGET_SDK
FullScreen = FULLSCREEN
Orientation = ORIENTATION

谢谢:)

好的,我找到了答案:)

Dim doc As XmlDocument = New XmlDocument()
doc.Load("config.xml")
Dim root As XmlElement = doc.DocumentElement
PackageName = root.GetAttribute("id")
ProjectVersion = root.GetAttribute("version")
ProjectName = doc.GetElementsByTagName("name").Item(0).InnerText
For i = 0 To doc.GetElementsByTagName("preference").Count - 1
    If (doc.GetElementsByTagName("preference").Item(i).Attributes.Item(0).Value.Contains("fullscreen")) Then
        FullScreen = doc.GetElementsByTagName("preference").Item(i).Attributes.Item(1).Value
    ElseIf (doc.GetElementsByTagName("preference").Item(i).Attributes.Item(0).Value.Contains("orientation")) Then
        Orientation = doc.GetElementsByTagName("preference").Item(i).Attributes.Item(1).Value
    ElseIf (doc.GetElementsByTagName("preference").Item(i).Attributes.Item(0).Value.Contains("android-minSdkVersion")) Then
        MinSdk = doc.GetElementsByTagName("preference").Item(i).Attributes.Item(1).Value
    ElseIf (doc.GetElementsByTagName("preference").Item(i).Attributes.Item(0).Value.Contains("android-targetSdkVersion")) Then
        TargetSdk = doc.GetElementsByTagName("preference").Item(i).Attributes.Item(1).Value
    ElseIf (doc.GetElementsByTagName("preference").Item(i).Attributes.Item(0).Value.Contains("android-installLocation")) Then
        InstallLocation = doc.GetElementsByTagName("preference").Item(i).Attributes.Item(1).Value
    ElseIf (doc.GetElementsByTagName("preference").Item(i).Attributes.Item(0).Value.Contains("android-versionCode")) Then
        VersionCode = doc.GetElementsByTagName("preference").Item(i).Attributes.Item(1).Value
    End If
Next

正如评论中所讨论的,这里是示例代码,您可以使用它来使用 xpath

获取值
Sub test()
    Dim objXML, arrNodes, nodesXML, i

    Set objXML = CreateObject("MSXML2.DOMDocument.6.0")
    With objXML
        .SetProperty "SelectionLanguage", "XPath"
        .SetProperty "SelectionNamespaces", "xmlns:s='http://www.w3.org/ns/widgets'"
        .ValidateOnParse = True
        .Async = False
        .Load "C:\Users\pankaj.jaju\Desktop\test.xml"
    End With

    arrNodes = Array("/s:widget/s:preference[@name='android-minSdkVersion']/@value", _
                    "/s:widget/s:preference[@name='android-versionCode']/@value", _
                    "/s:widget/s:preference[@name='android-installLocation']/@value", _
                    "/s:widget/s:preference[@name='android-targetSdkVersion']/@value", _
                    "/s:widget/s:preference[@name='orientation']/@value", _
                    "/s:widget/s:preference[@name='fullscreen']/@value", _
                    "/s:widget/@id", _
                    "/s:widget/@version", _
                    "/s:widget/s:name")

    For i = LBound(arrNodes) To UBound(arrNodes)
        Set nodesXML = objXML.DocumentElement.SelectSingleNode(arrNodes(i))
        MsgBox nodesXML.Text
    Next

    Set nodesXML = Nothing: Set objXML = Nothing
End Sub