Above is a 444. The XML file, I want to load this file in EXCEL VBA, how to obtain PRODUCTINFO this node PRODUCT_NAME the value of the attribute "11"? To find the relevant data, load the XML file is not to use the following code:
The Set xDoc=CreateObject (" Microsoft. XMLDOM)
XDoc. Async=False
XDoc. Load (" D: \ 444. XML ")
Could you tell me how to write the code behind the need? I want to get the "11" value assigned to the variable "A1", display msgbox,
CodePudding user response:
Followed by thisMsgBox XML documents. SelectSingleNode ("//PRODUCTINFO "). The Attributes. The getNamedItem (" PRODUCT_NAME "). The Text
CodePudding user response:
Qq574221329Thank you very much for your reply, you can got value, I also want to ask about, such as I choose the XML file if there is no this node? What do I need to determine whether the file inside the nodes and attributes, and attribute if there's any value in it? Must know how to write code first?
CodePudding user response:
If an XML document. SelectSingleNode (node name). The Attributes. The getNamedItem (property names) Is Nothing Then 'object Is empty, without this attribute
If an XML document. SelectSingleNode (nodes) Is Nothing Then 'object Is empty there Is no this node
CodePudding user response:
Check out...CodePudding user response:
'tools/reference/select "Microsoft XML vb6.0 by use"Sub MyLoadXML ()
Dim oMyXmlDoc As DOMDocument
Dim oMyXmlNode As IXMLDOMNode
Dim A1 As String
'create a DOMdocument instance and loads the XML file
The Set oMyXmlDoc=New DOMDocument
OMyXmlDoc. Async=False
OMyXmlDoc. Load (ThisWorkbook. Path & amp; "\ 444. XML") "the path of the XML file, and here EXCLE placed in the same folder
'to create an XPATH query data XML file node
The Set oMyXmlNode=oMyXmlDoc. SelectSingleNode ("//PRODUCTINFO/@ PRODUCT_NAME ")
'if there is a confirmation to find the node
If oMyXmlNode Is Nothing Then
MsgBox "without lookup node," vbOKCancel
The Exit Sub
End the If
'showing the find nodes attribute data of
A1=oMyXmlNode. Text
MsgBox "PRODUCT_NAME attribute's value is:" & amp; A1
'removal
The Set oMyXmlNode=Nothing
The Set oMyXmlDoc=Nothing
End Sub
Above using the DOM and XPATH loads and query the general method, for reference only