2017-08-11 83 views
0

我有這個代碼的XML文件:Visual Basic中解析XML文件中使用XmlDocument的

<?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 

我使用此代碼來獲得項目名,包名和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 

謝謝:)

+0

你嘗試過什麼嗎?所需值的xpath是非常基本的,你應該能夠對它們進行編碼。 –

+0

@PankajJaju我編輯了我的問題:) –

回答

0

好吧,我找到答案:)

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 
+1

如果您使用xpaths來獲取所需的值,將會容易得多。這將清理你的代碼,如果你把xpaths放在一個數組中並且使用一個循環來遍歷數組(可伸縮代碼),它將更容易維護。 –

+0

@PankajJaju我不知道如何使用它:( –

+1

Hi Milad,I已經發布了使用xpath獲取值的示例代碼 –

1

正如在評論中討論,這裏是示例代碼,您可以使用使用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