fork(1) 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. import javax.xml.parsers.SAXParser;
  11. import javax.xml.parsers.SAXParserFactory;
  12.  
  13. /* Name of the class has to be "Main" only if the class is public. */
  14. class Ideone
  15. {
  16. public static void main (String[] args) throws java.lang.Exception
  17. {
  18. Thread t = new Thread(new ParsingExample());
  19. t.start();
  20. }
  21.  
  22. static class ParsingExample implements Runnable {
  23. public void run(){
  24. try{
  25. final InputStream is = new ByteArrayInputStream( getXML().getBytes( ) );
  26. final SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
  27.  
  28. final MyHandler handler = new MyHandler();
  29. parser.parse(is, handler);
  30.  
  31. for(ElementContainer ec : handler.getElements()){
  32. System.out.println(ec);
  33. }
  34.  
  35. }
  36. catch(Exception ex){ex.printStackTrace();}
  37. }
  38.  
  39. private String getXML(){
  40. return "<?xml version='1.0' encoding='UTF-8'?> <rootElement> <property name='ireport.zoom' value='1.0'/> <property name='ireport.x' value='0'/> <property name='ireport.y' value='0'/> <queryString> <![CDATA[SELECT * FROM ORDERS]]> </queryString> <field name='ORDERID' class='java.lang.Integer'/> <field name='CUSTOMERID' class='java.lang.String'/> <field name='EMPLOYEEID' class='java.lang.Integer'/> <field name='ORDERDATE' class='java.sql.Timestamp'/> <field name='REQUIREDDATE' class='java.sql.Timestamp'/> <field name='SHIPPEDDATE' class='java.sql.Timestamp'/> <field name='SHIPVIA' class='java.lang.Integer'/> <field name='FREIGHT' class='java.math.BigDecimal'/> <field name='SHIPNAME' class='java.lang.String'/> <field name='SHIPADDRESS' class='java.lang.String'/> <field name='SHIPCITY' class='java.lang.String'/> <field name='SHIPREGION' class='java.lang.String'/> <field name='SHIPPOSTALCODE' class='java.lang.String'/> <field name='SHIPCOUNTRY' class='java.lang.String'/> <background> <band splitType='Stretch'/> </background> <columnHeader> <band height='20' splitType='Stretch'> <staticText> <reportElement uuid='da31a9d4-8ee7-481d-8b51-270539a2fdec' x='460' y='0' width='92' height='20'/> <textElement> <font isBold='true' isItalic='true' isUnderline='true'/> </textElement> <text><![CDATA[SHIPPEDDATE]]></text> </staticText> </band> </columnHeader> <detail> <band height='20' splitType='Stretch'> <textField> <reportElement uuid='60fb99da-64ef-4bf9-8a96-687c433be35a' x='460' y='0' width='92' height='20'/> <textElement> <font isBold='true' isItalic='true' isUnderline='true'/> </textElement> <textFieldExpression><![CDATA[$F{SHIPPEDDATE}]]></textFieldExpression> </textField> </band> </detail> </rootElement>";
  41. }
  42.  
  43.  
  44. private class ElementContainer {
  45. private final Map<String, String> attributes;
  46. private final String name;
  47. private String value = "";
  48.  
  49. public ElementContainer(final String name){
  50. attributes = new HashMap<>();
  51. this.name = name;
  52. }
  53.  
  54. public void addAttribute(final String name, final String value){
  55. attributes.put(name, value);
  56. }
  57.  
  58. public void appendValue(final String value){
  59. this.value = this.value.concat(value);
  60. }
  61.  
  62. public String toString(){
  63. final StringBuilder sb = new StringBuilder();
  64. sb.append("element : ")
  65. .append(name)
  66. .append("\nelement value : ")
  67. .append(!value.trim().isEmpty() ? value.trim() : "null")
  68. .append("\n");
  69.  
  70. if(attributes.isEmpty()){
  71. sb.append("attribute name : null")
  72. .append("\nattribute value : null");
  73. }
  74. else{
  75. for(Map.Entry<String, String> entry : attributes.entrySet()){
  76. sb.append("attribute name : ")
  77. .append(entry.getKey())
  78. .append("\nattribute value : ")
  79. .append(entry.getValue())
  80. .append("\n");
  81. }
  82. }
  83. sb.append("\n");
  84.  
  85. return sb.toString();
  86. }
  87. }
  88. private class MyHandler extends DefaultHandler {
  89.  
  90. private final List<ElementContainer> elements = new ArrayList<>();
  91. private ElementContainer activeElement;
  92.  
  93. public List<ElementContainer> getElements(){
  94. return elements;
  95. }
  96.  
  97. @Override
  98. public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
  99.  
  100. activeElement = new ElementContainer(qName);
  101.  
  102. for(int i = 0; i < attributes.getLength(); ++i){
  103. activeElement.addAttribute(attributes.getQName(i), attributes.getValue(i));
  104. }
  105.  
  106. super.startElement(uri, localName, qName, attributes);
  107. }
  108.  
  109. @Override
  110. public void characters(char[] ch, int start, int length) throws SAXException {
  111. activeElement.appendValue(String.valueOf(ch, start, length));
  112. super.characters(ch, start, length);
  113. }
  114.  
  115. @Override
  116. public void endElement(String uri, String localName, String qName) throws SAXException {
  117.  
  118. elements.add(activeElement);
  119. }
  120. }
  121.  
  122. }
  123. }
Success #stdin #stdout 0.12s 381184KB
stdin
Standard input is empty
stdout
element : property
element value : null
attribute name : name
attribute value : ireport.zoom
attribute name : value
attribute value : 1.0


element : property
element value : null
attribute name : name
attribute value : ireport.x
attribute name : value
attribute value : 0


element : property
element value : null
attribute name : name
attribute value : ireport.y
attribute name : value
attribute value : 0


element : queryString
element value : SELECT * FROM ORDERS
attribute name : null
attribute value : null

element : field
element value : null
attribute name : name
attribute value : ORDERID
attribute name : class
attribute value : java.lang.Integer


element : field
element value : null
attribute name : name
attribute value : CUSTOMERID
attribute name : class
attribute value : java.lang.String


element : field
element value : null
attribute name : name
attribute value : EMPLOYEEID
attribute name : class
attribute value : java.lang.Integer


element : field
element value : null
attribute name : name
attribute value : ORDERDATE
attribute name : class
attribute value : java.sql.Timestamp


element : field
element value : null
attribute name : name
attribute value : REQUIREDDATE
attribute name : class
attribute value : java.sql.Timestamp


element : field
element value : null
attribute name : name
attribute value : SHIPPEDDATE
attribute name : class
attribute value : java.sql.Timestamp


element : field
element value : null
attribute name : name
attribute value : SHIPVIA
attribute name : class
attribute value : java.lang.Integer


element : field
element value : null
attribute name : name
attribute value : FREIGHT
attribute name : class
attribute value : java.math.BigDecimal


element : field
element value : null
attribute name : name
attribute value : SHIPNAME
attribute name : class
attribute value : java.lang.String


element : field
element value : null
attribute name : name
attribute value : SHIPADDRESS
attribute name : class
attribute value : java.lang.String


element : field
element value : null
attribute name : name
attribute value : SHIPCITY
attribute name : class
attribute value : java.lang.String


element : field
element value : null
attribute name : name
attribute value : SHIPREGION
attribute name : class
attribute value : java.lang.String


element : field
element value : null
attribute name : name
attribute value : SHIPPOSTALCODE
attribute name : class
attribute value : java.lang.String


element : field
element value : null
attribute name : name
attribute value : SHIPCOUNTRY
attribute name : class
attribute value : java.lang.String


element : band
element value : null
attribute name : splitType
attribute value : Stretch


element : band
element value : null
attribute name : splitType
attribute value : Stretch


element : reportElement
element value : null
attribute name : height
attribute value : 20
attribute name : width
attribute value : 92
attribute name : uuid
attribute value : da31a9d4-8ee7-481d-8b51-270539a2fdec
attribute name : y
attribute value : 0
attribute name : x
attribute value : 460


element : font
element value : null
attribute name : isUnderline
attribute value : true
attribute name : isItalic
attribute value : true
attribute name : isBold
attribute value : true


element : font
element value : null
attribute name : isUnderline
attribute value : true
attribute name : isItalic
attribute value : true
attribute name : isBold
attribute value : true


element : text
element value : SHIPPEDDATE
attribute name : null
attribute value : null

element : text
element value : SHIPPEDDATE
attribute name : null
attribute value : null

element : text
element value : SHIPPEDDATE
attribute name : null
attribute value : null

element : text
element value : SHIPPEDDATE
attribute name : null
attribute value : null

element : reportElement
element value : null
attribute name : height
attribute value : 20
attribute name : width
attribute value : 92
attribute name : uuid
attribute value : 60fb99da-64ef-4bf9-8a96-687c433be35a
attribute name : y
attribute value : 0
attribute name : x
attribute value : 460


element : font
element value : null
attribute name : isUnderline
attribute value : true
attribute name : isItalic
attribute value : true
attribute name : isBold
attribute value : true


element : font
element value : null
attribute name : isUnderline
attribute value : true
attribute name : isItalic
attribute value : true
attribute name : isBold
attribute value : true


element : textFieldExpression
element value : $F{SHIPPEDDATE}
attribute name : null
attribute value : null

element : textFieldExpression
element value : $F{SHIPPEDDATE}
attribute name : null
attribute value : null

element : textFieldExpression
element value : $F{SHIPPEDDATE}
attribute name : null
attribute value : null

element : textFieldExpression
element value : $F{SHIPPEDDATE}
attribute name : null
attribute value : null

element : textFieldExpression
element value : $F{SHIPPEDDATE}
attribute name : null
attribute value : null