This tutorial teaches how to read and differentiate different type of XML Nodes. You might also interested to read parsing JSON tutorial at Read JSON with JAVA using Google-gson library.
There are 3 important XML data parts.
There are 3 important XML data parts.
- Element - Elements have tags. In above example bookstore, book, title, author are elements
- Attribute - These are attributes for Elements. In above example category is attribute.
- Text - Simple text surrounded by tags
Program Flow :
- Read ROOT Element as Node
- If it is Text, go to 3rd step else go to 4th step.
- print text value
- If it is Element, print all attributes and list children and send them to 2nd step
XOM Java Library :
By using traditional DOM and SAX parser we cannot implement above program flow. We need XOM parser. You can download XOM at http://www.cafeconleche.org/XOM/xom-1.2.10.jar
Java Code :
import java.io.*; import java.nio.charset.StandardCharsets; import nu.xom.*; public class XOMParserXML { public static void main(String[] args) { String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><bookstore>" + "\n<!-- Servlets --><book category=\"CHILDREN\">" + "\n <title>Harry Potter</title>" + "\n <author>J K. Rowling</author>" + "\n <year>2005</year>" + "\n <price>29.99</price>" + "\n </book>" + "\n <book category=\"WEB\">" + "\n <title>Learning XML</title>" + "\n <author>Erik T. Ray</author>" + "\n <year>2003</year>" + "\n <price>39.95</price>" + "\n </book>" + "\n</bookstore>"; System.out.println(xml); // Remove All Spaces xml = trimSpaces(xml); try { Builder builder = new Builder(); InputStream stream = new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8)); Document doc = builder.build(stream); // Get Root Element Element root = doc.getRootElement(); // List Children for Root Element listChildren(root, 0); } catch (ParsingException ex) { System.out.println(args[0] + " is not well-formed."); System.out.println(ex.getMessage()); } catch (IOException ex) { System.out.println(ex); } } public static void listChildren(Node current, int depth) { printSpaces(depth); String data = ""; // If it is element if (current instanceof Element) { Element temp = (Element) current; data = ": " + temp.getQualifiedName(); // print Attributes for (int i = 0; i < temp.getAttributeCount(); i++) { Attribute attribute = temp.getAttribute(i); String attValue = attribute.getValue(); attValue = attValue.replace('\n', ' ').trim(); if (attValue.length() >= 20) { attValue = attValue.substring(0, 17) + "..."; } data += "\r\n "; data += attribute.getQualifiedName(); data += "="; data += attValue; } System.out.println(current.getClass().getName() + data); // iterate child nodes for (int i = 0; i < current.getChildCount(); i++) { listChildren(current.getChild(i), depth+1); } } //If it is text else if (current instanceof Text) { String value = current.getValue(); value = value.replace('\n', ' ').trim(); data = ": " + value; System.out.println(current.getClass().getName() + data); } } // Method to remove all space and new lines public static String trimSpaces(String input) { BufferedReader reader = new BufferedReader(new StringReader(input)); StringBuffer result = new StringBuffer(); try { String line; while ( (line = reader.readLine() ) != null) result.append(line.trim()); return result.toString(); } catch (IOException e) { throw new RuntimeException(e); } } // Method to print spaces private static void printSpaces(int n) { for (int i = 0; i < n; i++) { System.out.print(" "); } } }
0 comments:
Post a Comment