从xml中读取多行

时间:2012-08-14 10:56:49

标签: java xml saxparser

我正在尝试使用sax解析器从java中的xml文件中获取数据。我成功地获得了少量数据,但是当数据变得太大并且在多行中时,它只提供两行数据,而不是所有行。我正在尝试使用代码 -

InputStreamReader isr = new InputStreamReader(is);
InputSource source = new InputSource(isr);
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(true);
SAXParser parser = factory.newSAXParser();
XMLReader xr = parser.getXMLReader();
GeofenceParametersXMLHandler handler = new GeofenceParametersXMLHandler();
xr.setContentHandler(handler);
xr.parse(source);

我的GeofenceParametersXMLHandler是 -

private boolean inTimeZone = false;
private boolean inCoordinate = false;
private boolean outerBoundaryIs = false;
private boolean innerBoundaryIs = false;
private String timeZone;
private List<String> innerCoordinates = new ArrayList<String>();
private String outerCoordinates;

public String getTimeZone() {
    return timeZone;
}

public List<String> getInnerCoordinates() {
    return innerCoordinates;
}

public String getOuterCoordinates() {
    return outerCoordinates;
}

@Override
public void characters(char[] ch, int start, int length) throws SAXException {
    super.characters(ch, start, length);
    if (this.inTimeZone) {
        this.timeZone = new String(ch, start, length);
        this.inTimeZone = false;
    }

    if (this.inCoordinate && this.innerBoundaryIs) {
        this.innerCoordinates.add(new String(ch, start, length));
        this.inCoordinate = false;
        this.innerBoundaryIs = false;
    }

    if (this.inCoordinate && this.outerBoundaryIs) {
        this.outerCoordinates = new String(ch, start, length);
        this.inCoordinate = false;
        this.outerBoundaryIs = false;
    }
}

@Override
public void endElement(String uri, String localName, String name) throws SAXException {
    super.endElement(uri, localName, name);
}

@Override
public void startDocument() throws SAXException {
    super.startDocument();
}

@Override
public void startElement(String uri, String localName, String name, Attributes attributes) throws SAXException {
    super.startElement(uri, localName, name, attributes);

    if (localName.equalsIgnoreCase("timezone")) {
        this.inTimeZone = true;
    }

    if (localName.equalsIgnoreCase("outerBoundaryIs")) {
        this.outerBoundaryIs = true;
    }

    if (localName.equalsIgnoreCase("innerBoundaryIs")) {
        this.innerBoundaryIs = true;
    }

    if (localName.equalsIgnoreCase("coordinates")) {
        this.inCoordinate = true;
    }
}

xml文件是 -

<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2"
xmlns:gx="http://www.google.com/kml/ext/2.2">

<Placemark>
   <name>gx:altitudeMode Example</name>
   <timezone>EASTERN</timezone>
   <Polygon>
      <extrude>1</extrude>
      <altitudeMode>relativeToGround</altitudeMode>
      <outerBoundaryIs>
      <LinearRing>
         <coordinates>
        -77.05788457660967,38.87253259892824,100 
        -77.05465973756702,38.87291016281703,100 
        -77.05315536854791,38.87053267794386,100 
        -77.05552622493516,38.868757801256,100 
        -77.05844056290393,38.86996206506943,100 
        -77.05788457660967,38.87253259892824,100
      </coordinates>
    </LinearRing>
  </outerBoundaryIs>
</Polygon>

我总是有两行坐标数据。但当他们在一条线上时,我得到了完整的数据。如何在多行中获取完整数据?

先谢谢。

1 个答案:

答案 0 :(得分:7)

characters()方法不一定会一次性给你所有的文本数据(这是一个非常常见的错误概念,顺便说一句)。

正确的方法是连接对字符()的连续调用返回的所有数据(使用StringBuilder或类似的)。调用endElement()方法后,您可以将该文本缓冲区视为已完成并按此处理。

来自doc:

  

Parser将调用此方法来报告每个字符块   数据。 SAX解析器可以在单个中返回所有连续的字符数据   块,或者他们可能将它分成几个块

通常您会看到,对于小型XML文档,只需调用characters()即可。但是,随着您的XML文档大小的增加,您会发现由于缓冲等原因,您将开始接收多个调用。因此,每次处理的电话似乎都是不完整的。