0

Actually i need to replace some of the tags in source xml and write the files as new one. Here my code works fine but now am not able to open the output xml. In the output xml i have some tamil words. Is it the reason for file not opening

 public class dxml {

  public static  StringBuffer sb = new StringBuffer() ;

public static void main(String [] args) throws Exception {
    File xmlFile = new File("/home/dev702/Desktop/axl/Data Entry.xml");
    BufferedReader br = new BufferedReader(
                new FileReader("/home/dev702/Desktop/axl/Data Entry.xml"));
    String line = null;
    int linecount = 1; 
    FileWriter fw;
    BufferedWriter bw = null;
    fw = new FileWriter("/home/dev702/Desktop/axl/Data_Entry_OPT.xml") ;
    bw = new BufferedWriter(fw);
    while((line = br.readLine())!= null)
        {
           if(linecount > 2)
            {
                line = line.replaceAll("Data_x0020_Entry_x0020_Date",
                                                                "DataEntryDate");
             //bw.write(line);
            }    
         bw.write(line);
         linecount++;
         System.out.println(line);
      }
    bw.close();
    fw.close();
  }
}
10
  • whats the error you are getting? Commented Feb 27, 2014 at 12:21
  • am not getting any error but the new file not opening Commented Feb 27, 2014 at 12:23
  • which editor you are using? Commented Feb 27, 2014 at 12:24
  • am not doing many things am just replacing a word with new word Commented Feb 27, 2014 at 12:25
  • xmlFile variable is useless in your code Commented Feb 27, 2014 at 12:25

1 Answer 1

1

If you want to transform XML to another form of XML you should be using XSLT's to achieve this. Java has support for transforming two documents...below is a code snippet of how to acheive this.

The premise really is you get your original XML into a document, set up the XSLT to use and transform it into another document.

The scope of using XSLT is outside of this reply. I recommend using Altova's excellent XMLSpy for testing your XSLT's.

public class Mapper {


public Document convert(Document originalDocument, Resource xsltResource) throws TransformerException, ParserConfigurationException,
        JAXBException, IOException, SAXException {

    /**
     * You'll need to create your documentBuilder to build the new document.
     */
    DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
    documentBuilderFactory.setNamespaceAware(true);
    DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();

    /**
     * Set up your transformer factory, you'll need to pass your XSLT file in as an inputstream
     * I've passed it in here as a method arg and it's a Spring Resource but you can do it however you like.
     */
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory
            .newTransformer(new StreamSource(xsltResource.getInputStream()));

    /**
     * Set the encoding to avoid headaches.
     */
    transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");


    /**
     * Create a BAoS to hold your original document.
     */
    ByteArrayOutputStream os = new ByteArrayOutputStream();
    transformer.transform(new DOMSource(originalDocument), new StreamResult(os));

    /**
     * Do the transformation.
     */
    return documentBuilder.parse(new InputSource(new StringReader(os.toString("UTF-8"))));

   }
}
Sign up to request clarification or add additional context in comments.

2 Comments

for this where can i get input file and output file..can you plz elaborate
Hi, so in your case the input file is /home/dev702/Desktop/axl/Data Entry.xml the XSLT would also be a file. You'd need to convert your XML File into a Document object. Google can help you do this and your for your XLST file you'd need to get it as an InputStream. Your output (after transforming) would be a Document object which you could then write out to a file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.