Wrox Programmer Forums
Go Back   Wrox Programmer Forums > XML > XML
|
XML General XML discussions.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the XML section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old June 14th, 2005, 05:00 AM
Registered User
 
Join Date: Jun 2005
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default append/insert data to xml file

hi,

i would like to append my xml file/insert new data to xml file. i use appendChild but it never works. I also want to ask about methode isSupported in Node.
the method is boolean isSupported(String feature, String version)
but i don't know what they mean by feature here, can someone explain about this method too.thank you

this is my xml file:

<?xml version="1.0" encoding="iso-8859-1"?>
<logins>
    <user id="x" system="y" envlib="a" env="2" prefix="SPW" depot="NL2" proddiv="FD" lang="NED" />
    <user id="r" system="a" envlib="aO" env="2" prefix="SPW" depot="NL2" proddiv="FD" lang="NED" />
    <user id="t" system="s7" envlib="w" env="4" prefix="SPR" depot="ASD" proddiv="AV" lang="NED" />

</logins>

this is my code:

String strFileName = getServletContext().getRealPath("/xml/user/users.xml");
   File file = new File(strFileName);
   Document doc = null;
   try{

       DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
       DocumentBuilder db = dbf.newDocumentBuilder();
       doc = db.parse(file);
       }
   catch(IOException e){
       System.out.println(e);
       }

    Element root = doc.getDocumentElement();
    String nodeValue = root.getNodeName();
    out.println(root.getParentNode().getNodeName());


    Element newElement = doc.createElement("user");
    newElement.setAttributeNode(doc.createAttribute("i d"));
    newElement.setAttribute("id", "andhiez");
    newElement.setAttributeNode(doc.createAttribute("s ystem"));
    newElement.setAttribute("system", "andhiez");
    newElement.setAttributeNode(doc.createAttribute("e nvlib"));
    newElement.setAttribute("envlib", "andhiez");
    newElement.setAttributeNode(doc.createAttribute("e nv"));
    newElement.setAttribute("env", "andhiez");
    newElement.setAttributeNode(doc.createAttribute("p refix"));
    newElement.setAttribute("prefix", "andhiez");
    newElement.setAttributeNode(doc.createAttribute("d epot"));
    newElement.setAttribute("depot", "andhiez");
    newElement.setAttributeNode(doc.createAttribute("p roddiv"));
    newElement.setAttribute("proddiv", "andhiez");
    newElement.setAttributeNode(doc.createAttribute("l ang"));
    newElement.setAttribute("lang", "andhiez");
    root.appendChild(newElement);


 
Old June 29th, 2005, 06:01 AM
Authorized User
 
Join Date: May 2005
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hello m8, you've probably sorted this issue out by now but just in case I thought I would add some code in anyway!

This is obviously using C# - not sure what code you are using but here it is.

Your XML file saved as users.xml in my temp directory:
<?xml version="1.0" encoding="iso-8859-1"?>
<logins>
    <user id="x" system="y" envlib="a" env="2" prefix="SPW" depot="NL2" proddiv="FD" lang="NED" />
    <user id="r" system="a" envlib="aO" env="2" prefix="SPW" depot="NL2" proddiv="FD" lang="NED" />
    <user id="t" system="s7" envlib="w" env="4" prefix="SPR" depot="ASD" proddiv="AV" lang="NED" />
</logins>

C# code

//Create an XML Document
XmlDocument doc = new XmlDocument();

//Load in the users xml file
try
{
    doc.Load(@"c:\Temp\users.xml");
}
catch(Exception err)
{
    //Oh dear the document probably wasn't well written
    Response.Write("error: " + err.Message);
}

//Create your new node that you want to add the attributes to
XmlNode newNode = doc.CreateElement("user");

//Create a new attribute assign the value and append it to the new node.
//Repeat this until all attributes have been added
XmlAttribute newAttribute = doc.CreateAttribute("id");
newAttribute.Value = "andhiez";
newNode.Attributes.Append(newAttribute);

//Append the new node to the document
doc.DocumentElement.AppendChild(newNode);

//Save the document
doc.Save(@"c:\Temp\users.xml");

Document now looks like this:
<?xml version="1.0" encoding="iso-8859-1"?>
<logins>
  <user id="x" system="y" envlib="a" env="2" prefix="SPW" depot="NL2" proddiv="FD" lang="NED" />
  <user id="r" system="a" envlib="aO" env="2" prefix="SPW" depot="NL2" proddiv="FD" lang="NED" />
  <user id="t" system="s7" envlib="w" env="4" prefix="SPR" depot="ASD" proddiv="AV" lang="NED" />
  <user id="andhiez" />
</logins>



 
Old July 4th, 2005, 02:23 AM
Registered User
 
Join Date: Jul 2005
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi All,

I want to create(write XML) file which is going to have a huge data (currently the XML file created is of 2mb)

which approach would be faster
1) XML TextWriter
OR
2) String Builder

Thanks
 
Old July 4th, 2005, 02:49 AM
mhkay's Avatar
Wrox Author
 
Join Date: Apr 2004
Posts: 4,962
Thanks: 0
Thanked 292 Times in 287 Posts
Default

2Mb isn't what I would describe as "huge" - I'd reserve that word for anything above 1Gb.

Writing the output at the level of SAX events (e.g. TextWriter) is asking the system to do more of the work, such as escaping special characters (ampersand, <). Writing the output at the level of character strings will be faster if and only if your application can do this work more efficiently than the system can.



Michael Kay
http://www.saxonica.com/
Author, XSLT Programmer's Reference and XPath 2.0 Programmer's Reference
 
Old April 18th, 2008, 10:48 AM
Authorized User
 
Join Date: Apr 2008
Posts: 26
Thanks: 6
Thanked 0 Times in 0 Posts
Default

Hi,

I looking for a asp.net vb code to add a new node to an existing XML file. I see C codes everywhere but no vb codes. Is that possible to transfer the C code here to vb code?

Thanks,

Norman

 
Old April 18th, 2008, 11:13 AM
Friend of Wrox
 
Join Date: Nov 2007
Posts: 1,243
Thanks: 0
Thanked 245 Times in 244 Posts
Default

Well the API you use with VB.NET is exactly the same as the API you use with C#.
Here is an example in VB.NET that creates and adds a 'foo' element:
Code:
Dim doc as New XmlDocument()
doc.Load("file.xml")
Dim foo As XmlElement = doc.CreateElement("foo")
foo.InnerText = "bar"
doc.DocumentElement.AppendChild(foo)
doc.Save("file.xml")
--
  Martin Honnen
  Microsoft MVP - XML





Similar Threads
Thread Thread Starter Forum Replies Last Post
append a xml node from one file to another pravind XSLT 5 August 22nd, 2009 09:15 AM
append in xml file rapraj XML 3 October 11th, 2007 02:58 AM
Append Information to a XML file YoungLuke C# 2 July 26th, 2007 02:15 PM
insert data param into xml from external file alexshiell XSLT 0 January 24th, 2006 01:47 PM
append data to xml andhiez General .NET 0 June 14th, 2005 04:15 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.