简体   繁体   English

如何在Java中使用Sax / Dom解析器读取xml文件?

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

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. 我想使用sax / dom解析器从xml读取数据,但是我不知道如何在Java中实现代码。 Any helps are highly appreciated... 任何帮助都受到高度赞赏...

Below texts about the requirements: 以下有关要求的文字:

1/ xml file: 1 / xml文件:

<?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: 2 /预期结果:

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? 关于使用Java程序进行sax / dom解析器技术的任何想法? Thanks in advance. 提前致谢。

The SAX example tutorial pertaining to this topic is a pretty good start. 与此主题相关的SAX示例教程是一个很好的开始。


As seen in the SAXParser Documentation , one approach to parsing is to use a DefaultHandler . SAXParser文档中所见,一种解析方法是使用DefaultHandler

The three "events" you primarily care about and need to override are startElement , endElement and characters . 您主要关心并需要覆盖的三个“事件”是startElementendElementcharacters

Inside of startElement you'll capture the qName (element) for the tag and it's associated Attributes . startElement内部,您将捕获标签及其关联的AttributesqName (元素)。

Inside of characters you'll capture the tag value (element value). characters内部,您将捕获标签值(元素值)。

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. 您可以使用endElement打印/存储该信息。


You'd start with a DefaultHandler extension 您将从DefaultHandler扩展开始

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. 并相应地为每个事件提供实施。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM