0

Input XML

<Employees>
    <Employee>
        <EmpId>101398</EmpId>
        <Name>ABC</Name>
    </Employee>
    <Employee>
        <EmpId>101399</EmpId>
        <Name>XYZ</Name>
    </Employee>
<Employees>


Output XML
String : "<Employee><EmpId>101398</EmpId><Name>ABC</Name></Employee>"

Note: I do not want to use XSLT. I would be thankful for your answer.

marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459

1 Answers1

0
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import org.w3c.dom.Element;
import java.io.File;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.io.StringWriter;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;



public class XMLFormatter {
    public static void main(String[] args) throws Exception{
        File fXmlFile = new File("src/a.xml");
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(fXmlFile);

        doc.getDocumentElement().normalize();

        System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

        NodeList nList = doc.getElementsByTagName("Employee");

        System.out.println("----------------------------");

        for (int temp = 0; temp < nList.getLength(); temp++) {

            System.out.println(nodeToString(nList.item(temp)));

        }
    }

    private static String nodeToString(Node node) {
        StringWriter sw = new StringWriter();
        try {
          Transformer t = TransformerFactory.newInstance().newTransformer();
          t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
          t.setOutputProperty(OutputKeys.INDENT, "yes");
          t.transform(new DOMSource(node), new StreamResult(sw));
        } catch (TransformerException te) {
          System.out.println("nodeToString Transformer Exception");
        }
        return sw.toString();
      }


}

The Running code for your problem a.xml contains your xml deatils

Atul Rai
  • 140
  • 10
  • While this may be correct (I haven't checked it), it would probably benefit the OP and future readers much more if you would _explain_ the concepts behind your solution. – Jim Garrison Jul 09 '13 at 15:52
  • In my case there are 3 components , middle one gets xml from the first and we process that XML and put each employee details in one Map (Employee as in above XML example) ,then we ve 2 use template likely 2 b XSLT or apache velocity to transform it into JSON which is accepted by the 3rd component.We send each map to the template. Main AIM of the template is to make it configurable in future i.e even if we change the XML there shouldn't be any change in the logic at JAVA level,only changes should be in template . Hope you understand my requirement. – Gamini Ramaraju Jul 13 '13 at 06:13
  • In the above example, It would be great if we could break XML without hard coding the "Employee" tag .Please suggest me if there is any way we could do it without hardcoding. and thank u for the solution it worked :) – Gamini Ramaraju Jul 13 '13 at 06:14
  • just get your template 1.Read the node name (in this case node will come "Employee") 2.pass the node name ..so it will depend on template ant it will not be hard coded – Atul Rai Jul 13 '13 at 18:12