dynamically generating XML prob

Discussion in 'XML' started by natkhatbandar, Dec 29, 2004.

  1. Hi,
    I am generating an XML document dynamically using DOM.
    I am creating an element using IXMLDOMDocument::createElement() and
    then calling IXMLDOMElement::appendChild() for the parent element. For
    attributes, I use IXMLDOMDocument2::createAttribute() and then
    IXMLDOMElement::setAttributeNode().

    My problem is that for each of the elements, an additional "xmlns"
    attribute is added with no value (xmlns=""). How can I avoid getting
    this extra attribute from being inserted? Note that I am not putting a
    prefix for the element names.


    --- Sample XML generated-----------------
    <MyRoot xmlns="http://www.xyz.com/abc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.xyz.com/abc abc.xsd">
    <MyElement1 xmlns="" attrib1="v1"/>
    ..
    ..
    </MyRoot>
     
    natkhatbandar, Dec 29, 2004
    #1
    1. Advertisements

  2. Sounds as if you are using MSXML, with MSXML you should use
    createNode(1, 'element-name', 'namespaceURI')
    if you want to create elements in a namespace. If you use
    createElement('element-name')
    then the element is created in the null namespace and thus when the
    document is serialized the
    <element-name xmlns="">
    needs to be added to properly serialize the document.

    So you want (JScript syntax)
    var rootElement = xmlDocument.createNode(
    1,
    'MyRoot',
    'http://www.xyz.com/abc'
    );
    var myElement = xmlDocument.createNode(
    1,
    'MyElement1',
    'http://www.xyz.com/abc'
    );

    If you are not using MSXML but a W3C DOM Level 2 compliant
    implementation then you need to use
    var rootElement = xmlDocument.createElementNS(
    'http://www.xyz.com/abc',
    'MyRoot'
    );
    var myElement = xmlDocument.createElementNS(
    'http://www.xyz.com/abc',
    'MyElement1'
    );
     
    Martin Honnen, Dec 29, 2004
    #2
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.