need a help to create XML node with text and sub child.

Discussion in 'XML' started by erbrmn@gmail.com, Mar 5, 2008.

  1. Guest

    Hi folks

    I need a help to create XML node with text and sub child.

    --------------------------------------------------------

    I want to create "ADDR" node with text and child node.
    (First child node and then text.)

    <?xml version="1.0" encoding="UTF-8" ?>
    - <ROOT>
    - <NODE>
    <child-1 attr="attr-value">This is child text</child-1>
    - <ADDR>
    <postcode>12345</postcode>
    Address information
    </ADDR>
    </NODE>
    </ROOT>

    --------------------------------------------------------

    I can create "ADDR" node with text and child node. But first text and
    then child node.
    I can not change the location of text and child node of "ADDR" node.

    <?xml version="1.0" encoding="UTF-8" ?>
    - <ROOT>
    - <NODE>
    <child-1 attr="attr-value">This is child text</child-1>
    - <ADDR>
    Address information
    <postcode>12345</postcode>
    </ADDR>
    </NODE>
    </ROOT>

    ------------------------------------------------------------------------------------
    This is code which I did (ASP)
    ------------------------------------------------------------------------------------

    Set xmlDoc = Server.CreateObject("Microsoft.XMLDOM")

    If (xmlDoc.childNodes.length = 0) Then

    Set objProcInstr = xmlDoc.createProcessingInstruction("xml",
    "version=""1.0"" encoding=""UTF-8""")
    xmlDoc.appendChild objProcInstr

    Set root = xmlDoc.createNode("element", "ROOT", "")
    xmlDoc.appendChild(root)

    Set node = xmlDoc.createNode("element", "NODE", "")
    xmlDoc.documentElement.appendChild(node)

    Set child = xmlDoc.createNode("element", "child-1", "")
    Set objAttr = xmlDoc.createAttribute("attr")
    child.setAttribute "attr","attr-value"
    child.text = "This is child text"
    node.appendChild(child)

    Set child = xmlDoc.createNode("element", "ADDR", "")
    node.appendChild(child)
    Set sub_child = xmlDoc.createNode("element", "postcode", "")
    sub_child.Text = "12345"
    child.Text = "Address information"
    child.appendChild(sub_child)

    End If

    xmlDoc.save (Server.Mappath("newxml.xml"))

    -----------------------
    , Mar 5, 2008
    #1
    1. Advertising

  2. wrote:

    > I want to create "ADDR" node with text and child node.
    > (First child node and then text.)
    >
    > <?xml version="1.0" encoding="UTF-8" ?>
    > - <ROOT>
    > - <NODE>
    > <child-1 attr="attr-value">This is child text</child-1>
    > - <ADDR>
    > <postcode>12345</postcode>
    > Address information
    > </ADDR>


    Set addr = xmlDoc.createElement("ADDR")
    Set postcode = xmlDoc.createElement("postcode")
    postcode.text = "12345"
    addr.appendChild postcode
    addr.appendChild xmlDoc.createTextNode("Address information")


    Note that your XML is poorly structured, mixed contents of an ADDR
    element with a child element and a child text node is not a good
    structure, you might want to use a second child element instead of the
    text node.

    --

    Martin Honnen
    http://JavaScript.FAQTs.com/
    Martin Honnen, Mar 5, 2008
    #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. Replies:
    0
    Views:
    1,442
  2. Bryan Ax
    Replies:
    6
    Views:
    541
    Peter Flynn
    May 17, 2006
  3. Tjerk Wolterink
    Replies:
    2
    Views:
    1,418
    Dimitre Novatchev
    Aug 24, 2006
  4. Replies:
    1
    Views:
    1,201
    Joe Kesselman
    Nov 1, 2006
  5. Ben
    Replies:
    2
    Views:
    877
Loading...

Share This Page