我試圖解析這個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-space-to-return-a-sequence-of-normalized-strings
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
我在做什麼錯?
更新
我有一個解決辦法了,但是這不能成爲解決方案。下面的幾行表明我如何把綰從類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
}
我寧願有一個堅實的解決方案。
'正常化空間()'帶前緣和後空白和空白字符(包括新行)其它序列轉換爲單個空格字符。作爲你的結果不具有'anschrift'元素的文本內容的線之間的空間,必須的東西吃之前你換行*'正常化空間()'得到完成其工作。 – Markus