473,378 Members | 1,441 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Need Urgent Help regarding XML

I have one XML file with thousands of contacts inside it,each contact is taken as a single node. i just want to split the file into two xml files such that first 100 contacts should be in the first file and the remaining in the second file. I am very new to this field and need this help immediately. please give me the entire code for this along with the description.
Thanks in advance.
Aug 29 '07 #1
8 1544
phvfl
173 Expert 100+
Hi Amit,

What language are you wanting to use to perform the transform?

This would be possible using XSLT or something like .NET (amongst others).

If using XSLT you would perform two different transforms, one for each resulting file.
Aug 29 '07 #2
jkmyoung
2,057 Expert 2GB
I don't think XSLT would be the smart option here, as the overhead added by creating a DOM structure would probably not be worth it. I suggest just copying a file line by line, keeping track of how many contacts, (presumably <contact> nodes) you come into contact with. Once you hit the desired 100, close off this file, by writing the closing elements.

Start the next file with the corresponding opening elements to start the list again, and then copy the rest of the source file into the 2nd result file.

If you're really uncertain of how to work and parse XML, you could try using a SAX parser, which has a lot less overhead as well.
Aug 29 '07 #3
phvfl
173 Expert 100+
I don't think XSLT would be the smart option here, as the overhead added by creating a DOM structure would probably not be worth it. I suggest just copying a file line by line, keeping track of how many contacts, (presumably <contact> nodes) you come into contact with. Once you hit the desired 100, close off this file, by writing the closing elements.

Start the next file with the corresponding opening elements to start the list again, and then copy the rest of the source file into the 2nd result file.

If you're really uncertain of how to work and parse XML, you could try using a SAX parser, which has a lot less overhead as well.
Each to there own. If this is a one off then certainly do it manually. If this is something that needs to be done repeatedly then either SAX or XSLT would be able to do this quickly, once the initial code had been done. I opted for XSLT as this is what I am more familiar with.
Aug 29 '07 #4
Hi Amit,

What language are you wanting to use to perform the transform?

This would be possible using XSLT or something like .NET (amongst others).

If using XSLT you would perform two different transforms, one for each resulting file.
Hi , i am using C#, please give the full code. Once again Thanx in advance
Aug 30 '07 #5
phvfl
173 Expert 100+
Hi , i am using C#, please give the full code. Once again Thanx in advance
Hi,

Sorry for taking so long to respond, I had to install VS and learn enough C# to do this (being a VB.NET dev). The steps that need to be performed are:
  1. Select required nodes into an XmlNodeList
  2. Create an empty document
  3. Create a node in that document to fill with contacts
  4. Loop through selected contacts and add to node
  5. Add the populated node to the document
  6. Save the document
  7. Repeat for the remaining nodes

This is very crude as it is the first C# coding that I have done:
Expand|Select|Wrap|Line Numbers
  1.         public void parseXml()
  2.         {
  3.             // Create a new XmlDocument.
  4.             XmlDocument doc = new XmlDocument();
  5.  
  6.             // Load XML into the document, use Load method to load a file.
  7.             doc.LoadXml("<doc><book><title>IT</title><author>S. King</author></book>" +
  8.                 "<book><title>The Shining</title><author>S. King</author></book></doc>");
  9.  
  10.             // Create two XmlNodeLists to hold the nodes for the two different files.
  11.             XmlNodeList list1;
  12.             XmlNodeList list2;
  13.  
  14.             // Populate the node lists, this uses XPath and in this example
  15.             // selects the first node into list1 and the remainder into list2
  16.             list1 = doc.SelectNodes("/doc/book[position()<=1]");
  17.             list2 = doc.SelectNodes("/doc/book[position()>1]");
  18.  
  19.             // Send the lists to a function to build the documents and save them.
  20.             // The first argument is the node list, the second is the file name to save to.
  21.             saveFile(list1, "file1.xml");
  22.             saveFile(list2, "file2.xml");
  23.  
  24.         }
  25.  
The code to create and save the files is:

Expand|Select|Wrap|Line Numbers
  1.         static void saveFile(XmlNodeList nodelist, string filename)
  2.         {
  3.             // Create new XML document.
  4.             XmlDocument doc = new XmlDocument();
  5.             XmlNode node1;
  6.  
  7.             // Add XMl declaration.
  8.             doc.AppendChild(doc.CreateXmlDeclaration("1.0", "UTF-8", null));
  9.  
  10.             //Create node to populate with the nodes in the node list.
  11.             node1 = doc.CreateNode(XmlNodeType.Element, "root", null);
  12.  
  13.             // Loop through all the nodes.
  14.             for(int i=0;i<nodelist.Count;i++){
  15.  
  16.                 // Append the node from the node list, ImportNode is used so that 
  17.                 // the node being added is in the correct context for the document 
  18.                 // being created.
  19.  
  20.                 node1.AppendChild(doc.ImportNode(nodelist[i],true));
  21.             }
  22.  
  23.             // Append the completed node to the document.
  24.             doc.AppendChild(node1);
  25.  
  26.             // Save the document.
  27.             doc.Save(filename);
  28.         }
  29.  
  30.  
You will need to change node names to match the document structure that you wish to use but the fundementals are as above.
Sep 2 '07 #6
Hi,

Sorry for taking so long to respond, I had to install VS and learn enough C# to do this (being a VB.NET dev). The steps that need to be performed are:
  1. Select required nodes into an XmlNodeList
  2. Create an empty document
  3. Create a node in that document to fill with contacts
  4. Loop through selected contacts and add to node
  5. Add the populated node to the document
  6. Save the document
  7. Repeat for the remaining nodes

This is very crude as it is the first C# coding that I have done:
Expand|Select|Wrap|Line Numbers
  1.         public void parseXml()
  2.         {
  3.             // Create a new XmlDocument.
  4.             XmlDocument doc = new XmlDocument();
  5.  
  6.             // Load XML into the document, use Load method to load a file.
  7.             doc.LoadXml("<doc><book><title>IT</title><author>S. King</author></book>" +
  8.                 "<book><title>The Shining</title><author>S. King</author></book></doc>");
  9.  
  10.             // Create two XmlNodeLists to hold the nodes for the two different files.
  11.             XmlNodeList list1;
  12.             XmlNodeList list2;
  13.  
  14.             // Populate the node lists, this uses XPath and in this example
  15.             // selects the first node into list1 and the remainder into list2
  16.             list1 = doc.SelectNodes("/doc/book[position()<=1]");
  17.             list2 = doc.SelectNodes("/doc/book[position()>1]");
  18.  
  19.             // Send the lists to a function to build the documents and save them.
  20.             // The first argument is the node list, the second is the file name to save to.
  21.             saveFile(list1, "file1.xml");
  22.             saveFile(list2, "file2.xml");
  23.  
  24.         }
  25.  
The code to create and save the files is:

Expand|Select|Wrap|Line Numbers
  1.         static void saveFile(XmlNodeList nodelist, string filename)
  2.         {
  3.             // Create new XML document.
  4.             XmlDocument doc = new XmlDocument();
  5.             XmlNode node1;
  6.  
  7.             // Add XMl declaration.
  8.             doc.AppendChild(doc.CreateXmlDeclaration("1.0", "UTF-8", null));
  9.  
  10.             //Create node to populate with the nodes in the node list.
  11.             node1 = doc.CreateNode(XmlNodeType.Element, "root", null);
  12.  
  13.             // Loop through all the nodes.
  14.             for(int i=0;i<nodelist.Count;i++){
  15.  
  16.                 // Append the node from the node list, ImportNode is used so that 
  17.                 // the node being added is in the correct context for the document 
  18.                 // being created.
  19.  
  20.                 node1.AppendChild(doc.ImportNode(nodelist[i],true));
  21.             }
  22.  
  23.             // Append the completed node to the document.
  24.             doc.AppendChild(node1);
  25.  
  26.             // Save the document.
  27.             doc.Save(filename);
  28.         }
  29.  
  30.  
You will need to change node names to match the document structure that you wish to use but the fundementals are as above.



Hi ,
Once again thanx for the help , by the way how many years of exp do you have in this field.
Sep 4 '07 #7
phvfl
173 Expert 100+
Hi,

Possibly not as long as you would expect, I have been doing javascript and client side stuff for about 18 months and server side for about 14 months (10 months .NET, the rest classic ASP)
Sep 4 '07 #8
Hi,

Possibly not as long as you would expect, I have been doing javascript and client side stuff for about 18 months and server side for about 14 months (10 months .NET, the rest classic ASP)
Hi, If you dont mind ,i want to be bit personal to you .
What are doing professionally , where u stays(Country), i mean your brief description.About your family.
Sep 17 '07 #9

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: mch2k2 | last post by:
Hello All I have just started working on Pyhton. I need urgent help regarding Python Network Programming. I want the elctronic version of the Book: Foundations of Python Network programming by...
8
by: Mike | last post by:
Hello, I have a few rather urgent questions that I hope someone can help with (I need to figure this out prior to a meeting tomorrow.) First, a bit of background: The company I work for is...
8
by: Tim::.. | last post by:
Can someone please tell me why I keep getting the following error for some of my web application users but not others??? Even though the application runs from a central webserver??? Thanks for...
16
by: | last post by:
Hi all, I have a website running on beta 2.0 on server 2003 web sp1 and I keep getting the following error:- Error In:...
0
by: Miguel Dias Moura | last post by:
Hello, I am working on an Asp.Net 2.0 / SQL 2005 web site. I am using profile to save the users info on the database. For example, I have the following structure: Public Structure Name...
4
by: archana | last post by:
Hi all, I am having one confusion regarding invoking web method of web service asychronously through windows applicaiton. What i am doing is i am having one long runing web method whose one...
5
by: meenu_susi | last post by:
doubt regarding select box....urgent in the below code i am getting data from database..according to the condition.. i want the available data in database to get displayed in selectbox.. for...
1
by: yogeeswar | last post by:
I need to write a SP to decommission one of the value from all tables.but all the deletions need to be made at a time, if problems occurs at any delete statement all the previously deleted rows need...
1
by: pdesh3 | last post by:
Hi, I have a form which is used to search the patient details. When I search for a patient it displays a list of patients' details in the subform. I want to implement the functionality...
5
by: =?Utf-8?B?SGVyYg==?= | last post by:
I need a Table object in my RDLC with 6 columns on the top several rows, but 1 column on the bottom two rows: COLUMN1 COLUMN2 COLUMN3 COLUMN4 COLUMN5 COLUMN6 COLUMN1 COLUMN2 COLUMN3 ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.