Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6018095
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T03:13:30+00:00 2026-05-23T03:13:30+00:00

I’m trying to get an attribute id ( fileID ) from my XML document

  • 0

I’m trying to get an attribute id (fileID) from my XML document to use as the filename for my XML split. The split works I just need to extract the fileID to use as the name.

[EDITED] I can read the attribute now but it doesn’t create the last xml file. So in my example it create the first 2 files with the correct name but last fileID “000154OP.XML” isn’t created. Can Anyone Help?

This is my xml document

<root>
 <envelope fileID="000152OP.XML">
   <record id="850">
   </record>
</envelope>
<envelope fileID="000153OP.XML">
  <record id="850">
  </record>
</envelope>
<envelope fileID="000154OP.XML">
  <record id="850">
  </record>
</envelope>
</root>

And here’s my Java code

    public static void splitXMLFile (String file) throws Exception {         
    String[] temp;
    String[] temp2;
    String[] temp3;
    String[] temp4;
    String[] temp5;
    String[] temp6;
    File input = new File(file);         
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();         
    Document doc = dbf.newDocumentBuilder().parse(input);
    XPath xpath = XPathFactory.newInstance().newXPath();          
    NodeList nodes = (NodeList) xpath.evaluate("//root/envelope", doc, XPathConstants.NODESET);          
    int itemsPerFile = 1;         

    Node staff = doc.getElementsByTagName("envelope").item(0);

    NamedNodeMap attr = staff.getAttributes();
    Node nodeAttr = attr.getNamedItem("fileID");
    String node = nodeAttr.toString();
    temp = node.split("=");
    temp2 = temp[1].split("^\"");
    temp3 = temp2[1].split("\\.");

    Document currentDoc = dbf.newDocumentBuilder().newDocument();         
    Node rootNode = currentDoc.createElement("root");   
    File currentFile = new File("C:\\XMLFiles\\" + temp3[0]+ ".xml"); 

    for (int i=1; i <= nodes.getLength(); i++) {             
        Node imported = currentDoc.importNode(nodes.item(i-1), true);             
        rootNode.appendChild(imported); 

        Node staff2 = doc.getElementsByTagName("envelope").item(i);
        NamedNodeMap attr2 = staff2.getAttributes();
        Node nodeAttr2 = attr2.getNamedItem("fileID");
        String node2 = nodeAttr2.toString();
        temp4 = node2.split("=");
        temp5 = temp4[1].split("^\"");
        temp6 = temp5[1].split("\\.");

        if (i % itemsPerFile == 0) { 

            writeToFile(rootNode, currentFile);                  
            rootNode = currentDoc.createElement("root");    
            currentFile = new File("C:\\XMLFiles\\" + temp6[0]+".xml");


        }         
    }          
    writeToFile(rootNode, currentFile);     
}    

 private static void writeToFile(Node node, File file) throws Exception {         
     Transformer transformer = TransformerFactory.newInstance().newTransformer();         
     transformer.transform(new DOMSource(node), new StreamResult(new FileWriter(file)));     
 } 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T03:13:31+00:00Added an answer on May 23, 2026 at 3:13 am

    There is a lot of duplication in your code but I have a solution that removes a lot of it. I know there are less complex solutions (for example I don’t think the if (i % itemsPerFile == 0) logic is required, but I do not know all of your requirements, so I have left it in.

    The main problems you have were overwriting the last file with wrong data but also that your looping logic was duplicated. A good rule of thumb I go by is whenever I think I might have to duplicate code there is something wrong. Your logic was considering the first <envelope> separately to the remaining <envelope> elements, whereas they should be considered as a group of 3. Then your logic need only to apply the same searching, splitting, matching, importing, etc… to each element in turn.

    What complicated matters, is that your input XML file had the same <record id="850"> for each <envelope>. I changed mine to 850, 851 and 852. Running your original code, produced 3 files, 000152OP.xml, 000153OP.xml and 000154OP.xml, but the first one contained the 851 record. So I immediately knew the looping logic was incorrect.

    A simpler solution is detailed below, which given your input XML file as the argument produces 3 output files in the same directory (I removed the C:\ hard-coding for simplicity), each with the correct <record> element.

    import java.io.*;
    import java.util.Random;
    import org.w3c.dom.*;
    import javax.xml.parsers.*;
    import javax.xml.xpath.*;
    import javax.xml.transform.*;
    import javax.xml.transform.dom.*;
    import javax.xml.transform.stream.*;
    
    public class SplitXML {
        public static void main(String[] args) throws Exception {
            File input = new File(args[0]);
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            Document doc = dbf.newDocumentBuilder().parse(input);
            XPath xpath = XPathFactory.newInstance().newXPath();
            NodeList nodes = (NodeList) xpath.evaluate("//root/envelope", doc, XPathConstants.NODESET);
            int itemsPerFile = 1;
    
            Document currentDoc = dbf.newDocumentBuilder().newDocument();
    
            for (int i=0; i < nodes.getLength(); i++) {
                Node rootNode = currentDoc.createElement("root");
    
                Node imported = currentDoc.importNode(nodes.item(i), true);
                rootNode.appendChild(imported);
    
                Node staff = doc.getElementsByTagName("envelope").item(i);
                NamedNodeMap attr = staff.getAttributes();
                Node nodeAttr = attr.getNamedItem("fileID");
                String filename = nodeAttr.getNodeValue();
                String[] fileParts = filename.split("\\.");
    
                if (i % itemsPerFile == 0) {
                    File currentFile = new File(fileParts[0] + "." + fileParts[1].toLowerCase());
                    writeToFile(rootNode, currentFile);
                }
            }
        }
    
        private static void writeToFile(Node node, File file) throws Exception {
            Transformer transformer = TransformerFactory.newInstance().newTransformer();
            transformer.transform(new DOMSource(node), new StreamResult(new FileWriter(file)));
        }
    }
    

    You should read up on Node and String::split as there was unnecessary extra code where a native method already exists (for example [Node::getNodeValue()][3]).

    Edit: The source for creating 1000 <envelope> elements that I used to test the above code:

    import java.io.*;
    
    public class CreateXML {
        public static void main(String[] args) throws Exception {
            FileWriter fstream = new FileWriter(new File("split.xml"));
            BufferedWriter out = new BufferedWriter(fstream);
            out.write("<root>");
            for (int i = 0; i < 1000; i++) {
                out.write("<envelope fileID=\"000" + i +"P.XML\"><record id=\"" + i + "\"></record></envelope>\n");
            }
            out.write("</root>");
            out.close();
        }
    }
    

    I ran java CreateXML to create the input file split.xml and then java SplitXML split.xml to create the 1000 files.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
I have just tried to save a simple *.rtf file with some websites and
I am trying to loop through a bunch of documents I have to put
I have a bunch of posts stored in text files formatted in yaml/textile (from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I need to clean up various Word 'smart' characters in user input, including but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.