Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > XML > need a help to create XML node with text and sub child.

Reply
Thread Tools

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

 
 
erbrmn@gmail.com
Guest
Posts: n/a
 
      03-05-2008
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"))

-----------------------
 
Reply With Quote
 
 
 
 
Martin Honnen
Guest
Posts: n/a
 
      03-05-2008
http://www.velocityreviews.com/forums/(E-Mail Removed) 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/
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Death To Sub-Sub-Sub-Directories! Lawrence D'Oliveiro Java 92 05-20-2011 06:50 AM
Recognising Sub-Items and sub-sub items using xslt Ben XML 2 09-19-2007 09:35 AM
xsl variable $node/text() but $node can non-node-set help! Tjerk Wolterink XML 2 08-24-2006 03:28 AM
How to set the node indent property between the parent node and the leaf node viveknatani@gmail.com ASP .Net 0 02-13-2006 07:11 PM
How to drag a sub-node to another node in TreeView? wolf ASP .Net 0 08-18-2004 11:51 PM



Advertisments