如何解析这为XML提供Java.xml.xpath



我正在尝试解析此xml:

<?xml version="1.0" encoding="UTF-8"?>
<veranstaltungen>
  <veranstaltung id="201611211500#25045271">
    <titel>Mal- und Zeichen-Treff</titel>
    <start>2016-11-21 15:00:00</start>
    <veranstaltungsort id="20011507">
      <name>Freizeitclub - ganz unbehindert </name>
      <anschrift>Macht los e.V.
Lipezker Straße 48
03048 Cottbus
</anschrift>
      <telefon>xxxx xxxx </telefon>
      <fax>0355 xxxx</fax>
[...]
</veranstaltungen>

您可以看到,其中一些文本具有空格甚至线路破坏。我对节点anschrift的文本有问题,因为我需要在数据库中找到正确的位置数据。问题是,返回的字符串为:

Macht los e.V.Lipezker Straße 4803048 Cottbus

而不是:

Macht los e.V. Lipezker Straße 48 03048 Cottbus

我知道解析的正确方法应该与normalie-space()一起使用,但我无法算出如何做。我尝试了:

// Does not work; afaik because xpath 1 normalizes just the first node
xPath.compile("normalize-space(veranstaltungen/veranstaltung[position()=1]/veranstaltungsort/anschrift/text()"));
// Does not work
xPath.compile("veranstaltungen/veranstaltung[position()=1]/veranstaltungsort[normalize-space(anschrift/text())]"));

我还尝试了此处给出的解决方案:xpath normalize空间到返回 - 序列符号符号

xPathExpression = xPath.compile("veranstaltungen/veranstaltung[position()=1]/veranstaltungsort");
NodeList result = (NodeList) xPathExpression.evaluate(doc, XPathConstants.NODESET);
String normalize = "normalize-space(.)";
xPathExpression = xPath.compile(normalize);
int length = result.getLength();
for (int i = 0; i < length; i++) {
    System.out.println(xPathExpression.evaluate(result.item(i), XPathConstants.STRING));
}

system.out打印:

Macht los e.V.Lipezker Straße 4803048 Cottbus

我在做什么错?

update

我已经有解决方法,但这不是解决方案。以下几行显示了我如何将字符串从httpresponse组合在一起:

try (BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), Charset.forName(charset)))) {
  final StringBuilder stringBuilder = new StringBuilder();
  String              line;
  while ((line = reader.readLine()) != null) {
    // stringBuilder.append(line);
    // WORKAROUND: Add a space after each line
    stringBuilder.append(line).append(" ");
  }
  // Work with the red lines
}

我宁愿有一个可靠的解决方案。

最初,您似乎正在使用以下代码读取XML:

try (BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), Charset.forName(charset)))) {
  final StringBuilder stringBuilder = new StringBuilder();
  String              line;
  while ((line = reader.readLine()) != null) {
    stringBuilder.append(line);
  }
}

这是您的新线被吃掉的地方: readline() do 返回尾随的新线字符。如果您然后解析stringBuilder对象的内容,则会获得不正确的DOM,其中文本节点不包含XML的原始newlines。

感谢Markus的帮助,我能够解决这个问题。原因是BufferedReader丢弃线路断裂的readline()方法。以下代码对我有用(也许可以改进):

public Document getDocument() throws IOException, ParserConfigurationException, SAXException {
  final HttpResponse response = getResponse(); // returns a HttpResonse
  final HttpEntity   entity   = response.getEntity();
  final Charset      charset  = ContentType.getOrDefault(entity).getCharset();  
  // Not 100% sure if I have to close the InputStreamReader. But I guess so.
  try (InputStreamReader isr = new InputStreamReader(entity.getContent(), charset == null ? Charset.forName("UTF-8") : charset)) {
    return documentBuilderFactory.newDocumentBuilder().parse(new InputSource(isr));
  }
}

最新更新