XML visual basic 6

时间:2015-11-28 17:47:41

标签: xml xml-parsing vb6

我正在尝试将XML文件读入我们的VB6应用程序。我已经经历了很多代码试图获得"修饰符"对于每个"项目"在列表中,但只能同时检索它们。问题似乎是它们都被命名为相同!任何帮助将不胜感激! - KC

这是XML:

     <?xml version="1.0" encoding="ISO-8859-1"?>
    <GetNewOrdersResponse>
        <ResultCode>0</ResultCode>
        <Message>Ok</Message>
        <LocationID>436</LocationID>
        <StoreTimeGMT>2015-11-27 05:13:38</StoreTimeGMT>
        <TotalRecords>1</TotalRecords>
        <Tickets>
            <ticket>
        <order_id>5278</order_id>
        <type>TAKEOUT</type>
        <comments></comments>
        <customer>
            <customer_id></customer_id>
            <phone_id></phone_id>
            <firstname>Kyle</firstname>
            <lastname>Cross</lastname>
            <email>crosskg62@gmail.com</email>
            <phone>4438786137</phone>
        </customer>
        <created_timestamp>2015-11-26 23:19:47</created_timestamp>
        <requested_timestamp>2015-11-27 17:00:00</requested_timestamp>
        <items>
            <item id="21845" pos_id="5702">
                <name>Cheese Pizza</name>
                <size>12"</size>
                <quantity>1</quantity>
                <price>8.95</price>
                <comments></comments>
                <modifiers>
                    <modifier id="44347" pos_id="3252">
                        <name>Thick</name>
                        <price>0.00</price>
                        <quantity>1</quantity>
                    </modifier>
                    <modifier id="44349" pos_id="6000">
                        <name>Cheddar Cheese</name>
                        <price>0.00</price>
                        <quantity>whole_extra</quantity>
                    </modifier>
                    <modifier id="44350" pos_id="6003">
                        <name>Green Olives</name>
                        <price>0.50</price>
                        <quantity>left</quantity>
                    </modifier>
                    <modifier id="44351" pos_id="6005">
                        <name>Black Olives</name>
                        <price>0.50</price>
                        <quantity>right</quantity>
                    </modifier>
                    <modifier id="44353" pos_id="6009">
                        <name>Tomatoes</name>
                        <price>1.00</price>
                        <quantity>whole</quantity>
                    </modifier>
                    <modifier id="44355" pos_id="6013">
                        <name>Pineapple</name>
                        <price>0.50</price>
                        <quantity>right</quantity>
                    </modifier>
                </modifiers>
            </item>
            <item id="21782" pos_id="5001">
                <name>Big Burger</name>
                <size></size>
                <quantity>3</quantity>
                <price>8.95</price>
                <comments></comments>
                <modifiers>
                    <modifier id="44287" pos_id="3044">
                        <name>Well</name>
                        <price>0.00</price>
                        <quantity>1</quantity>
                    </modifier>
                    <modifier id="44306" pos_id="5100">
                        <name>Cole Slaw</name>
                        <price>0.00</price>
                        <quantity>1</quantity>
                    </modifier>
                </modifiers>
            </item>
            <item id="21805" pos_id="4700">
                <name>Fried Chicken Salad</name>
                <size></size>
                <quantity>1</quantity>
                <price>10.95</price>
                <comments></comments>
                <modifiers>
                    <modifier id="44318" pos_id="3030">
                        <name>Blue Cheese</name>
                        <price>0.00</price>
                        <quantity>1</quantity>
                    </modifier>
                </modifiers>
            </item>
        </items>
        <subtotal>49.25</subtotal>
        <tax>3.45</tax>
        <total>52.70</total>
        <PaymentType>CASH</PaymentType>
        <PaymentStatus>UNPAID</PaymentStatus>
    </ticket>
</Tickets>

以下是代码:

    Dim xmlDoc As DOMDocument
    Dim objNodeList As IXMLDOMNodeList
    Dim objNodeList2 As IXMLDOMNodeList
    Dim objProductNode As IXMLDOMNode
    Dim objQuantityNode As IXMLDOMNode
    Dim objNode As IXMLDOMNode
    Dim objNode2 As IXMLDOMNode
    Dim XMLurl As String

Dim strRet As String

Set xmlDoc = New DOMDocument
XMLurl = "c:\kyle.xml"

xmlDoc.async = False

If xmlDoc.Load(XMLurl) = False Then
    MsgBox ("XML LOAD ERROR")
Else
    i = 0
    Set objNodeList = xmlDoc.selectNodes("//items")
    For Each objNode In objNodeList

        mPrice = objNode.selectSingleNode("//price").Text
        mPrice = objNode.selectSingleNode("//price").Text
        mQty = objNode.selectSingleNode("//quantity").Text
        mQty = objNode.selectSingleNode("//size").Text

    Next 'objNode

    ' get the modifiers for that node
    Set objNodeList2 = xmlDoc.selectNodes("//Modifier")
    For Each objNode2 In objNodeList2

        mName = xmlDoc.selectSingleNode("//name").Text
        mPrice = xmlDoc.selectSingleNode("//price").Text
        mQty = xmlDoc.selectSingleNode("//quantity").Text

    Next objNode2
End If

1 个答案:

答案 0 :(得分:1)

选择节点后使用相对路径表达式,即更改

Set objNodeList = xmlDoc.selectNodes("//items")
For Each objNode In objNodeList

    mPrice = objNode.selectSingleNode("//price").Text
    mPrice = objNode.selectSingleNode("//price").Text
    mQty = objNode.selectSingleNode("//quantity").Text
    mQty = objNode.selectSingleNode("//size").Text

Next 'objNode

' get the modifiers for that node
Set objNodeList2 = xmlDoc.selectNodes("//Modifier")
For Each objNode2 In objNodeList2

    mName = xmlDoc.selectSingleNode("//name").Text
    mPrice = xmlDoc.selectSingleNode("//price").Text
    mQty = xmlDoc.selectSingleNode("//quantity").Text

Next objNode2

Set objNodeList = xmlDoc.selectNodes("//item")
For Each objNode In objNodeList

    mPrice = objNode.selectSingleNode("price").Text

    mQty = objNode.selectSingleNode("quantity").Text
    mQty = objNode.selectSingleNode("size").Text



' get the modifiers for that node
Set objNodeList2 = objNode.selectNodes(".//modifier")
For Each objNode2 In objNodeList2

    mName = xmlDoc.selectSingleNode("name").Text
    mPrice = xmlDoc.selectSingleNode("price").Text
    mQty = xmlDoc.selectSingleNode("quantity").Text

Next objNode2
Next