1

I'm trying to use vbscript to create a xml file using the following format

<?xml version="1.0" encoding="UTF-8"?>
<products xmlns="http://www.myurl.com">
<product>
<url>http://www.link.com</url>
</product>

I was able to add all the elements for "products", "product" and "url", but the problem is that I don't know how to add the xmlns to the products element.

Here is my code:

Set xmlDoc = CreateObject("Microsoft.XMLDOM")  
Set objRoot = xmlDoc.createElement("products")
xmlDoc.appendChild objRoot  

1 Answer 1

1

Create an attribute xmlns for the node:

Set xmlDoc = CreateObject("Msxml2.DOMDocument.6.0")
Set objRoot = xmlDoc.createElement("products")

Set xmlns = xmlDoc.createAttribute("xmlns")
xmlns.text = "http://www.myurl.com"
objRoot.setAttributeNode xmlns

xmlDoc.appendChild objRoot
Sign up to request clarification or add additional context in comments.

3 Comments

That almost worked, problem is that it also added the AttributeNode to the second element. How can I prevent that?
<?xml version="1.0" encoding="UTF-8"?> <products xmlns="myurl.com"> <product xmlns=""> <url>link</url> </product>
@Zulake Why do you consider this a problem?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.