Posts

Showing posts with the label Xpath

Merging multiple xmls using Java (Part-2)

Image
In the previous post we have discussed about how we can collect tags by name in two xmls. NodeList fNodes, sNodes;fNodes = fMsg.getElementsByTagName("person");sNodes = sMsg.getElementsByTagName("person"); Now we have fNodes and sNodes variables with elements by tag inside. We'll append the elements of one xml to the other xml. for (int i = 0; i < sNodes.getLength(); i = i + 1) {       Node n = (Node) fMsg.importNode(sNodes.item(i), true);       fNodes.item(i).getParentNode().appendChild(n);} Okay....!!!!!Everything is okay now.All we have to do is get the output from above implementation.First we'll create a StringWriter object and parse the fMsg object to it.Here we have to use the Transformer object in order to build the new xml. StringWriter buf = new StringWriter();Transformer transformer = TransformerFactory.newInstance().newTransformer();transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");tr...

Merging multiple xmls using Java (Part-1)

Image
In the previous post we have completed our implementation on the "xml splitter". So in this post we'll discuss about how we can merge multiple xmls using Java.  Assume you have following xmls. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>      <person>     <id>person0</id>     <name>name0</name>     <age>age0</age>  </person> <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <person>     <id>person1</id>     <name>name1</name>     <age>age1</age>  </person> When the xpath is given as //person you want these two xmls to be merged to one xml from <person></person> tags.The final output should look like this. <?xml version="1.0" encoding="UTF-8" standalone="yes"?>           <persons> ...

Split a large XML into pieces using Java(Part-2)

Image
In the previous post we discussed about building a parser for parsing xml. At the end of the previous post you are like "WTH is this new XmlErrorHandler() thing??".Basically what it does is that handle the errors inside the xml.(Missing tags and schema errors).So let's implement the error handler class. public class XmlErrorHandler implements ErrorHandler {       public void warning(SAXParseException e) throws SAXException {            //print the exception       }       public void error(SAXParseException e) throws SAXException {            //print the exception       }       public void fatalError(SAXParseException e) throws SAXException {           //print the exception       } } Okay now the fun part..!!We need to split the xml from <person></person>  t...

Split a large XML into pieces using Java(Part-1)

Image
You may sometimes need to split a large xml into small pieces using an xpath. The main idea of splitting an xml into small pieces is that you can make the xml more process-able and easy to use.So today we'll talk about how we can split an xml into pieces according to a given xpath using Java. Assume we have the following xml string. String xml="<?xml version="1.0" encoding="UTF-8" standalone="yes"?>           <persons>      <person>          <id>person0</id>          <name>name0</name>          <age>age0</age>      </person>      <person>          <id>person1</id>          <name>name1</name>          <age>age1</age>   ...