1

I have xml file with this codes:

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

And i have this variables:

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

I use this code to get ProjectName, PackageName and 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

But i can't get this values:

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

Thank you :)

2
  • Have you tried anything? The xpaths for the required values are very basic and you should be able to code them. Commented Aug 11, 2017 at 10:55
  • @PankajJaju i edited my question :) Commented Aug 11, 2017 at 13:16

2 Answers 2

1

As discussed in comments, here is the sample code which you can use to get the values using 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
Sign up to request clarification or add additional context in comments.

Comments

0

Ok i found answer :)

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

3 Comments

It would be much easier if you use xpaths to get the desired values. This will cleanup your code and it is much easier to maintain if you put the xpaths in an array and use a loop to traverse the array (scalable code)
@PankajJaju I do not know how to use it :(
Hi Milad, I have posted sample code which uses xpath to fetch values

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.