Need Urgent Help regarding XML

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • amitkumar11
    New Member
    • Aug 2007
    • 5

    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.
    Last edited by amitkumar11; Aug 29 '07, 06:38 AM. Reason: some points left.
  • phvfl
    Recognized Expert New Member
    • Aug 2007
    • 173

    #2
    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.

    Comment

    • jkmyoung
      Recognized Expert Top Contributor
      • Mar 2006
      • 2057

      #3
      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.

      Comment

      • phvfl
        Recognized Expert New Member
        • Aug 2007
        • 173

        #4
        Originally posted by jkmyoung
        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.

        Comment

        • amitkumar11
          New Member
          • Aug 2007
          • 5

          #5
          Originally posted by phvfl
          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

          Comment

          • phvfl
            Recognized Expert New Member
            • Aug 2007
            • 173

            #6
            Originally posted by amitkumar11
            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:
            Code:
                    public void parseXml()
                    {
                        // Create a new XmlDocument.
                        XmlDocument doc = new XmlDocument();
            
                        // Load XML into the document, use Load method to load a file.
                        doc.LoadXml("<doc><book><title>IT</title><author>S. King</author></book>" +
                            "<book><title>The Shining</title><author>S. King</author></book></doc>");
            
                        // Create two XmlNodeLists to hold the nodes for the two different files.
                        XmlNodeList list1;
                        XmlNodeList list2;
            
                        // Populate the node lists, this uses XPath and in this example
                        // selects the first node into list1 and the remainder into list2
                        list1 = doc.SelectNodes("/doc/book[position()<=1]");
                        list2 = doc.SelectNodes("/doc/book[position()>1]");
            
                        // Send the lists to a function to build the documents and save them.
                        // The first argument is the node list, the second is the file name to save to.
                        saveFile(list1, "file1.xml");
                        saveFile(list2, "file2.xml");
            
                    }
            The code to create and save the files is:

            Code:
                    static void saveFile(XmlNodeList nodelist, string filename)
                    {
                        // Create new XML document.
                        XmlDocument doc = new XmlDocument();
                        XmlNode node1;
            
                        // Add XMl declaration.
                        doc.AppendChild(doc.CreateXmlDeclaration("1.0", "UTF-8", null));
            
                        //Create node to populate with the nodes in the node list.
                        node1 = doc.CreateNode(XmlNodeType.Element, "root", null);
            
                        // Loop through all the nodes.
                        for(int i=0;i<nodelist.Count;i++){
            
                            // Append the node from the node list, ImportNode is used so that 
                            // the node being added is in the correct context for the document 
                            // being created.
            
                            node1.AppendChild(doc.ImportNode(nodelist[i],true));
                        }
            
                        // Append the completed node to the document.
                        doc.AppendChild(node1);
            
                        // Save the document.
                        doc.Save(filename);
                    }
            You will need to change node names to match the document structure that you wish to use but the fundementals are as above.

            Comment

            • amitkumar11
              New Member
              • Aug 2007
              • 5

              #7
              Originally posted by phvfl
              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:
              Code:
                      public void parseXml()
                      {
                          // Create a new XmlDocument.
                          XmlDocument doc = new XmlDocument();
              
                          // Load XML into the document, use Load method to load a file.
                          doc.LoadXml("<doc><book><title>IT</title><author>S. King</author></book>" +
                              "<book><title>The Shining</title><author>S. King</author></book></doc>");
              
                          // Create two XmlNodeLists to hold the nodes for the two different files.
                          XmlNodeList list1;
                          XmlNodeList list2;
              
                          // Populate the node lists, this uses XPath and in this example
                          // selects the first node into list1 and the remainder into list2
                          list1 = doc.SelectNodes("/doc/book[position()<=1]");
                          list2 = doc.SelectNodes("/doc/book[position()>1]");
              
                          // Send the lists to a function to build the documents and save them.
                          // The first argument is the node list, the second is the file name to save to.
                          saveFile(list1, "file1.xml");
                          saveFile(list2, "file2.xml");
              
                      }
              The code to create and save the files is:

              Code:
                      static void saveFile(XmlNodeList nodelist, string filename)
                      {
                          // Create new XML document.
                          XmlDocument doc = new XmlDocument();
                          XmlNode node1;
              
                          // Add XMl declaration.
                          doc.AppendChild(doc.CreateXmlDeclaration("1.0", "UTF-8", null));
              
                          //Create node to populate with the nodes in the node list.
                          node1 = doc.CreateNode(XmlNodeType.Element, "root", null);
              
                          // Loop through all the nodes.
                          for(int i=0;i<nodelist.Count;i++){
              
                              // Append the node from the node list, ImportNode is used so that 
                              // the node being added is in the correct context for the document 
                              // being created.
              
                              node1.AppendChild(doc.ImportNode(nodelist[i],true));
                          }
              
                          // Append the completed node to the document.
                          doc.AppendChild(node1);
              
                          // Save the document.
                          doc.Save(filename);
                      }
              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.

              Comment

              • phvfl
                Recognized Expert New Member
                • Aug 2007
                • 173

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

                Comment

                • amitkumar11
                  New Member
                  • Aug 2007
                  • 5

                  #9
                  Originally posted by phvfl
                  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.Abo ut your family.

                  Comment

                  Working...