Posts

Showing posts from December, 2016

Strings, I/O and formatting (Part-1)

Image
"Hey Isla Are you serious??Strings,IO and formatting???"Hmmm..yeah I agree with you.This is basic Java.Anyway no problem we'll go further and discuss and see how much you know about Java Strings,I/O and formatting.We'll start with a little background information about strings. Strings are immutable objects.(You know what I mean) String is a 16-bit Unicode character In java string is an object.You can create a new instance using new keyword String myString=new String(); This creates a new string object and assign it to myString reference variable.Cool..Now we need to assign a value to the new variable.Let's do it like this. myString="Hasitha"; And we are done.Now we have a string variable with a value.Is this the only way we can do this.Absolutely no.String class has a number of constructors.We can do the same thing as follows. String myString=new String("Hasitha"); String myString="Hasitha"; Hmmm...Note that there...

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...