0

我正在使用 excel 2007 vba 生成 xml。执行宏后,我得到这个输出 在此处输入图像描述

但我需要像这样的输出

在此处输入图像描述

我不想要 < 和 > 而不是 < 和 > 标签。我正在使用的宏是

enter code here

Dim xmlDoc As New MSXML2.DOMDocument30
Dim objNode As MSXML2.IXMLDOMNode
Dim personNode As MSXML2.IXMLDOMNode
Dim imagesNode As MSXML2.IXMLDOMNode
Set objNode = xmlDoc.createNode(NODE_PROCESSING_INSTRUCTION, "xml", "")
xmlDoc.appendChild objNode
Set objNode = xmlDoc.createNode(NODE_ELEMENT, "root", "")
Set personNode = xmlDoc.createNode(NODE_ELEMENT, "person", "")
objNode.appendChild personNode
Set imagesNode = xmlDoc.createNode(NODE_ELEMENT, "images", "")
personNode.appendChild imagesNode
imagesNode.Text = "<image href=" & Chr(34) & "file:///Users/d.jahr/Desktop/XML           Booklet/Bilder/pic1.jpg" & Chr(34) & "/>"
xmlDoc.appendChild objNode
Set objNode = Nothing
MsgBox xmlDoc.XML
xmlDoc.Save ActiveWorkbook.Path & "\" & "result.xml"

结束子

请帮我。

谢谢

4

1 回答 1

1

与其设置集合的文本,不如images考虑附加一个image子节点并设置href它的属性:

Sub test()
    Dim xmlDoc As New MSXML2.DOMDocument30
    Dim objNode As MSXML2.IXMLDOMNode
    Dim personNode As MSXML2.IXMLDOMNode
    Dim imagesNode As MSXML2.IXMLDOMNode

    Dim image1Node As MSXML2.IXMLDOMNode
    Dim namedItem As Object

    Set objNode = xmlDoc.createNode(NODE_PROCESSING_INSTRUCTION, "xml", "")
    xmlDoc.appendChild objNode
    Set objNode = xmlDoc.createNode(NODE_ELEMENT, "root", "")
    Set personNode = xmlDoc.createNode(NODE_ELEMENT, "person", "")
    objNode.appendChild personNode
    Set imagesNode = xmlDoc.createNode(NODE_ELEMENT, "images", "")
    personNode.appendChild imagesNode

    Set image1Node = xmlDoc.createNode(NODE_ELEMENT, "image", "")
    imagesNode.appendChild image1Node
    Set namedItem = xmlDoc.createAttribute("href")
    namedItem.Value = "file:///Users/d.jahr/Desktop/XMLBooklet/Bilder/pic1.jpg"
    image1Node.Attributes.setNamedItem namedItem

    xmlDoc.appendChild objNode
    Debug.Print xmlDoc.XML
End Sub

输出:

<?xml version="1.0"?>
<root>
   <person>
      <images>
         <image href="file:///Users/d.jahr/Desktop/XMLBooklet/Bilder/pic1.jpg"/> 
      </images>
   </person>
</root>
于 2013-01-27T16:41:35.133 回答