2

我正在尝试使用 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>

我总是得到两行坐标数据。但是当它们在单行时,我得到了完整的数据。如何在多行中获取完整的数据?

提前致谢。

4

1 回答 1

8

characters()方法不一定会一次性为您提供所有文本数据(这是一个非常常见的误解,顺便说一句)。

正确的方法是将连续调用 characters() 返回的所有数据连接起来(使用 aStringBuilder或类似方法)。一旦endElement()调用了您的方法,您就可以将该文本缓冲区视为完整的并对其进行处理。

从文档:

Parser 将调用此方法来报告每个字符数据块。SAX 解析器可以在单个块中返回所有连续的字符数据,也可以将其拆分为多个块

通常你会看到,对于一个小的 XML 文档,一个调用characters()就足够了。但是,随着您的 XML 文档的大小增加,您会发现由于缓冲等原因,您将开始收到多个调用。因此,每个单独处理的调用似乎都不完整。

于 2012-08-14T11:00:04.970 回答