dynamically generating XML prob

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

  1. Guest

    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>
    -----------------------------------------


    Thanks in advance.
    , Dec 29, 2004
    #1
    1. Advertising

  2. wrote:


    > 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?


    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
    http://JavaScript.FAQTs.com/
    Martin Honnen, Dec 29, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. -=Chris=-
    Replies:
    2
    Views:
    1,657
    -=Chris=-
    Nov 9, 2003
  2. John Edwards
    Replies:
    1
    Views:
    315
    Hermit Dave
    Jan 16, 2004
  3. Raghavendra
    Replies:
    0
    Views:
    345
    Raghavendra
    Sep 8, 2004
  4. Jon
    Replies:
    1
    Views:
    551
    Andy Dingley
    Feb 11, 2005
  5. Ian Pellew

    YAHP - Ref to a hash via xml::simple prob.

    Ian Pellew, Nov 24, 2004, in forum: Perl Misc
    Replies:
    0
    Views:
    71
    Ian Pellew
    Nov 24, 2004
Loading...

Share This Page