vb6 xml字符串到文本框

时间:2010-02-09 12:22:15

标签: xml vb6 textbox

任何人都可以帮我创建接受参数xml字符串的函数,并返回格式化的字符串作为响应,然后才能 例如:

<cars>
      <ford>
        <model>fiesta</model>
        <model>focus</model>
      </ford>
      <renault>
        <model>twingo</model>
        <model>clio</model>
      </renault>
</cars>

应该返回

cars:
     ford:
          model=fiesta
          model=focus
     renault:
          model=twingo
          model=clio

2 个答案:

答案 0 :(得分:1)

最简单的方法是使用MSXML库。例如,请查看以下教程:

http://msdn.microsoft.com/en-us/library/aa468547.aspx

它没有“完美打印”功能,可以完全按照您的要求进行操作,但编写一个循环遍历XML元素并输出格式化文本的函数非常简单。

答案 1 :(得分:1)

的VBScript:

set xmldoc = CreateObject("Microsoft.XMLDOM")
xmldoc.async = false
xmldoc.loadXML ""               & _
  "<cars>"                      & _
  "    <ford>"                  & _
  "      <model>fiesta</model>" & _
  "      <model>focus</model>"  & _
  "    </ford>"                 & _
  "    <renault>"               & _
  "      <model>twingo</model>" & _
  "      <model>clio</model>"   & _
  "    </renault>"              & _
  "</cars>"

test.value = buildStructure(xmlDoc.DocumentElement, "")

function buildStructure(xmlParent, identLevel)

  dim result, xmlNode
  result = identLevel & xmlParent.nodeName & ": " & chr(13)

    for each xmlNode in xmlParent.SelectNodes("*")
        if not xmlNode.SelectSingleNode("*") is nothing Then
           result = result & buildStructure(xmlNode, identLevel & "   ")
        else
           result = result & identLevel & "   " & _
                    xmlNode.nodeName & " = " & xmlNode.text & chr(13)
        End if
    next

    buildStructure = result

end function