tags:

views:

89

answers:

2

Can anybody help me to create function that will accept for parameter xml string and return a formatted string as response that after can Example:

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

should return

cars:
     ford:
          model=fiesta
          model=focus
     renault:
          model=twingo
          model=clio
+1  A: 

The easiest way is to use the MSXML library. Have a look, for example, at the following tutorial:

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

It does not have a "pretty print" function that does exactly what you want, but it's quite simple to write a function that loops through the XML elements and outputs a formatted text.

Heinzi
+1  A: 

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
Rubens Farias
Work like a charm. Thanks
Tomislav