tags:

views:

79

answers:

5

I was wondering if this was a proper XML syntax, because I need to remove a node in this document using VBscript and I really am not able to do it.

<?xml version="1.0" encoding="utf-8"?>
<dbm>
  <servers>
    <server name="PCTEST">
      <references>
        <database name="TES1" path="\C$\Build"/>
      </references>
    </server>
  </servers>
</dbm>
A: 

Looks like it, yeah.

Douwe Maan
A: 

Although i could not find XML document from the list, Direct Input with detecting automatically validates XML easily.

Edit: http://validator.w3.org/

Deniz Acay
A: 

It looks fine but if you want a handy (free) tool for validating & working with XML, Microsoft's XML Notepad is pretty useful.

One thing you might want to check for is that your file's encoding matches UTF-8: http://www.w3schools.com/XML/xml_encoding.asp .

Nick Gotch
+1  A: 

It might be.

Without a schema (like an xsd or a dtd) it is impossible to know for sure.

Some parsers/manipulators validate against a schema, and sometimes they are configured to use an external schema, which means your program may (earlier than you think) load some schemas and fail your requests to remove or manipulate the document in a way that the schema doesn't permit.

If you're not using a schema, or (worse) you're using an ad-hoc parser that you, or someone at your company wrote) then I'd suspect there's a bug in your parser.

geocar
+2  A: 

Try this:

Set xmldoc = CreateObject("Microsoft.XMLDOM")
xmldoc.async = false
xmldoc.loadXml _
  "<?xml version='1.0' encoding='utf-8'?>" & _
  "<dbm>" & _
  "  <servers>" & _
  "    <server name='PCTEST'>" & _
  "      <references>" & _
  "        <database name='TES1' path='\C$\Build'/>" & _
  "      </references>" & _
  "    </server>" & _
  "  </servers>" & _
  "</dbm>"

''// Removing a single node
Set nodeToBeRemoved = xmldoc.selectSingleNode("//database[@name='TES1']")
If Not nodeToBeRemoved Is Nothing Then
   nodeToBeRemoved.parentNode.RemoveChild nodeToBeRemoved
End if

''// Removing multiple nodes
Set nodesToBeRemoved = xmldoc.selectNodes("//database")
For Each nodeToBeRemoved In nodesToBeRemoved
    nodeToBeRemoved.parentNode.RemoveChild nodeToBeRemoved
Next

''// alert xmldoc.xml
Rubens Farias
Thank you worked, but I'm not sure to understand the syntax you used to select the node ("//database[@name='TES1']"). If I wanted to select all database nodes if I had several would I do SelectNodes("//database") only? Thanks for your answer
Apoc
You're right, `SelectNodes` will do the trick; I updated my answer to reflect that example. About sintax, that's a `XPath` expression: http://www.w3schools.com/Xpath/
Rubens Farias