How to read xml file using Sax/Dom parser in Java?

心不动则不痛 提交于 2019-12-12 02:49:55

问题


I would like to ask you a problem. I want to read data from xml using either sax/dom parser but I don't know how to implement code in java. Any helps are highly appreciated...

Below texts about the requirements:

1/ xml file:

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

2/ Expected Result:

element         : property
elment value    : null
attribute name  : name
attribute value : ireport.zoom 
attribute name  : value
attribute value : 1.0 

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

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

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

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

Any idea regarding to sax/dom parser technology using java program? Thanks in advance.


回答1:


The SAX example tutorial pertaining to this topic is a pretty good start.


As seen in the SAXParser Documentation, one approach to parsing is to use a DefaultHandler.

The three "events" you primarily care about and need to override are startElement, endElement and characters.

Inside of startElement you'll capture the qName (element) for the tag and it's associated Attributes.

Inside of characters you'll capture the tag value (element value).

By the time the tag closes you'll have captured all of the required data that you desire. You can use the endElement to print / store that information.


You'd start with a DefaultHandler extension

class MyHandler extends DefaultHandler {
  @Override
  public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
     //Capture tag name and attributes
     super.startElement(uri, localName, qName, attributes);  
  }

  @Override
 public void characters(char[] ch, int start, int length) throws SAXException {
     //Capture value.  
     super.characters(ch, start, length);  
  }

  @Override
  public void endElement(String uri, String localName, String qName) throws SAXException {
    //Print or store the information
  }
}

and provide the implementation for each event accordingly.



来源:https://stackoverflow.com/questions/24112813/how-to-read-xml-file-using-sax-dom-parser-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!