fork download
  1. /* package whatever; // don't place package name! */
  2.  
  3. import java.util.*;
  4. import java.lang.*;
  5. import java.io.*;
  6. import org.xml.sax.helpers.DefaultHandler;
  7. import org.xml.sax.Attributes;
  8. import org.xml.sax.SAXException;
  9. import javax.xml.parsers.ParserConfigurationException;
  10.  
  11. import javax.xml.parsers.DocumentBuilderFactory;
  12. import javax.xml.parsers.DocumentBuilder;
  13. import org.w3c.dom.*;
  14.  
  15. /* Name of the class has to be "Main" only if the class is public. */
  16. class Ideone
  17. {
  18. public static void main (String[] args) throws java.lang.Exception
  19. {
  20. Thread t = new Thread(new ParsingExample());
  21. t.start();
  22. }
  23.  
  24. static class ParsingExample implements Runnable {
  25. public void run(){
  26. try{
  27. final InputStream is = new ByteArrayInputStream( getXml2().getBytes( ) );
  28. final List<ElementContainer> elements = new ArrayList<>();
  29. final DocumentBuilder documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
  30. final Document document = documentBuilder.parse(is);
  31.  
  32. processNodes(document.getChildNodes(), null, elements);
  33.  
  34.  
  35. for(ElementContainer ec : elements){
  36. System.out.println(ec);
  37. }
  38. }
  39. catch(Exception ex){System.out.println(ex); ex.printStackTrace();}
  40. }
  41.  
  42. private void processNodes(final NodeList list, final ElementContainer parent, final List<ElementContainer> elements){
  43.  
  44. for(int i=0; i< list.getLength(); i++){
  45.  
  46. final Node node = list.item(i);
  47.  
  48.  
  49. if("#text".equals(node.getNodeName()) || "#cdata-section".equals(node.getNodeName())){
  50.  
  51. parent.setValue(node.getNodeValue());
  52. }
  53. else{
  54. final ElementContainer p = processNode(node, elements);
  55. processNodes(node.getChildNodes(), p, elements);
  56. }
  57. }
  58. }
  59. private ElementContainer processNode(Node node, List<ElementContainer> elements){
  60. final ElementContainer element = new ElementContainer(node);
  61.  
  62. //System.out.println(element);
  63.  
  64. if(node.hasAttributes()){
  65. final NamedNodeMap attrs = node.getAttributes();
  66. for(int k=0; k<attrs.getLength(); k++){
  67. final Node attr = attrs.item(k);
  68. element.addAttribute(attr.getNodeName(), attr.getNodeValue());
  69. }
  70. }
  71. elements.add(element);
  72. return element;
  73. }
  74.  
  75. private String getXml1(){
  76. return "<company> <staff id='1001' adress='new york'> <firstname>yong</firstname> <lastname>mook kim</lastname> <nickname>mkyong</nickname> <salary>100000</salary> </staff> </company>";
  77. }
  78.  
  79. private String getXml2(){
  80. return " <page><columnHeader><band height='20' splitType='Stretch'><text><![CDATA[HEADER]]></text></band></columnHeader><detail><band height='20' splitType='Stretch'><textFieldExpression><![CDATA[$F{VALUES}]]></textFieldExpression></band></detail><footer><band height='20' splitType='Stretch'><text><![CDATA[FOOTER]]></text></band></footer></page>";
  81. }
  82.  
  83. private class ElementContainer {
  84. private final Map<String, String> attributes;
  85. private final String name;
  86. private String value;
  87. private final short type;
  88.  
  89. public ElementContainer(final Node node){
  90. attributes = new HashMap<>();
  91. this.name = node.getNodeName();
  92. this.value = null == node.getNodeValue() ? "" : node.getNodeValue();
  93. this.type = node.getNodeType();
  94. }
  95.  
  96. public void addAttribute(final String name, final String value){
  97. attributes.put(name, value);
  98. }
  99.  
  100. public void setValue(String value){
  101. this.value = value;
  102. }
  103.  
  104. public String getValue(){
  105. return value;
  106. }
  107.  
  108. public String toString(){
  109.  
  110. final StringBuilder sb = new StringBuilder();
  111. sb
  112. .append("Type: ")
  113. .append(type)
  114. .append("\nName : ")
  115. .append(name)
  116.  
  117. .append("\nValue : ")
  118. .append(!value.trim().isEmpty() ? value.trim() : "null")
  119. .append("\n");
  120.  
  121. if(attributes.isEmpty()){
  122. sb.append("attributes : {null}");
  123. }
  124. else{
  125. for(Map.Entry<String, String> entry : attributes.entrySet()){
  126. sb.append("attribute name : ")
  127. .append(entry.getKey())
  128. .append("\nattribute value : ")
  129. .append(entry.getValue())
  130. .append("\n");
  131. }
  132. }
  133. sb.append("\n");
  134.  
  135. return sb.toString();
  136. }
  137. }
  138. }
  139. }
Success #stdin #stdout 0.12s 381312KB
stdin
Standard input is empty
stdout
Type: 1
Name : page
Value : null
attributes  : {null}

Type: 1
Name : columnHeader
Value : null
attributes  : {null}

Type: 1
Name : band
Value : null
attribute name : splitType
attribute value : Stretch
attribute name : height
attribute value : 20


Type: 1
Name : text
Value : HEADER
attributes  : {null}

Type: 1
Name : detail
Value : null
attributes  : {null}

Type: 1
Name : band
Value : null
attribute name : splitType
attribute value : Stretch
attribute name : height
attribute value : 20


Type: 1
Name : textFieldExpression
Value : $F{VALUES}
attributes  : {null}

Type: 1
Name : footer
Value : null
attributes  : {null}

Type: 1
Name : band
Value : null
attribute name : splitType
attribute value : Stretch
attribute name : height
attribute value : 20


Type: 1
Name : text
Value : FOOTER
attributes  : {null}