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 8866711
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:51:41+00:00 2026-06-14T16:51:41+00:00

i have a program to take a big xml file and validate it and

  • 0

i have a program to take a big xml file and validate it and then split it in smaller files. the issue am getting is that the encoding of the new files is UTF-8 .I need them in ISO-8859-1

herez the code

public class SplitMain {

public static void main(String [] args) throws Exception {
    validateInputFile("D:/sanket/cms_dev/XmlSplitSample/src/inputFile/");
    File input = new File("D:/sanket/cms_dev/XmlSplitSample/src/inputFile/sample.xml");
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    Document doc = dbf.newDocumentBuilder().parse(input);
    XPath xpath = XPathFactory.newInstance().newXPath();

    NodeList nodes = (NodeList) xpath.evaluate("//DataFile/Contact", doc, XPathConstants.NODESET);

    int itemsPerFile = 5;
    int fileNumber = 0;
    Document currentDoc = dbf.newDocumentBuilder().newDocument();
    Node rootNode = currentDoc.createElement("DataFile");
    File currentFile = new File("nufile"+fileNumber+".xml");
    for (int i=1; i <= nodes.getLength(); i++) {
        Node imported = currentDoc.importNode(nodes.item(i-1), true);
        rootNode.appendChild(imported);

        if (i % itemsPerFile == 0) {
            writeToFile(rootNode, currentFile);

            rootNode = currentDoc.createElement("DataFile");
            currentFile = new File("nufile"+(++fileNumber)+".xml");
            System.out.println(currentFile);
        }
    }

    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)));
}
private static void validateInputFile(String WORK_DIRECTORY)
{
    //String workingDir=config.getProperty(WORK_DIRECTORY);//comment for automating the process

    String workingDir=WORK_DIRECTORY;//added for automating the process

    String finalString = null;
    File folder = new File(workingDir);

    if (folder.isFile())
    {
        System.out.println("watever");
        return ;
    }

    String[] fileNameArray = folder.list();
    String xmlExtension=".xml";
    for (String fileName : fileNameArray) {



            try{
                //XMLtoString
                BufferedReader br = new BufferedReader(new FileReader(new File(workingDir + "/" +fileName))); 
                String line;
                StringBuilder stringBuilder = new StringBuilder();
                while((line=br.readLine())!= null)
                {
                    stringBuilder.append(line.trim()); 
                } 
                finalString = stringBuilder.toString();
                StringBuilder sb = new StringBuilder();

                if(finalString == null)
                    return;
               System.out.println(finalString);
                for(int i=0;i<finalString.length();i++)
                {
                   if (finalString.charAt(i) == '&')
                    {
                       sb.append("&amp;");

                    }
                    else
                    {
                        sb.append(finalString.charAt(i));
                    }
                }
                finalString=sb.toString();

                //StringToXML

                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();   
                System.out.println(finalString);
                DocumentBuilder builder=factory.newDocumentBuilder();
                Document document = builder.parse( new InputSource(new StringReader( finalString ) ) ); 
                TransformerFactory tranFactory = TransformerFactory.newInstance();   
                Transformer aTransformer = tranFactory.newTransformer(); 
                Source src = new DOMSource(document); 
                Result dest = new StreamResult( new File( workingDir + "/" +fileName) );
                aTransformer.transform( src, dest ); 

            }
                catch (Exception e) {
                e.printStackTrace();
                }



    }
}

}

  • 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-06-14T16:51:42+00:00Added an answer on June 14, 2026 at 4:51 pm

    You need to specify the Transformer encoding e.g.:

    transformer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
    

    and then write out using a stream (if you use a writer, it’ll go through another unwanted layer of encoding)

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

Sidebar

Related Questions

We have a program that take specific XML files and imports them, then changes
I have a program that will take input xml and print same xml as
I have a program that sorts big files by splitting them into chunks, sort
I have a program that's expected to take as input a few options to
I have a program that saves an image in a local directory and then
I have a program that I am trying to take a set of numbers
I have a program where I take a date from an RSS file and
I have a program that is going to take a password as input and
I have program that requires Python 3, but I develop Django and it uses
I have program that has a variable that should never change. However, somehow, it

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.