gpt4 book ai didi

java - Android 中的 Saxparser 异常

转载 作者:太空宇宙 更新时间:2023-11-03 10:28:12 25 4
gpt4 key购买 nike

我正在从这个 XML url 解析数据文本输入因用户而异。每当文本变量中有空格时,我都会收到此异常:

 org.apache.harmony.xml.ExpatParser$ParseException: At line 11, column 2: mismatched tag
org.apache.harmony.xml.ExpatParser.parseFragment(ExpatParser.java:520)
org.apache.harmony.xml.ExpatParser.parseDocument(ExpatParser.java:479)
org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:318)
org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:275)
gps.app.tkartor.XMLObjects.XMLParser.findStreet(XMLParser.java:99)

如果没有空格,它工作正常。解析代码:

public void findStreet(String searchWord) {
try {
url = new URL(
"http://maps.travelsouthyorkshire.com/iGNMSearchService.asmx/TextSearch?text="
+ searchWord + "&maxResults=100");
System.out.println(url.toString());
parserFactory = SAXParserFactory.newInstance();
parser = parserFactory.newSAXParser();
reader = parser.getXMLReader();
streetHandler = new StreetHandler();
reader.setContentHandler(streetHandler);
reader.parse(new InputSource(url.openStream()));//line 99
poi = streetHandler.getAllStreets();

} catch (Exception e) {
e.printStackTrace();
}
}

最佳答案

用'+'表示空格

http://maps.travelsouthyorkshire.com/iGNMSearchService.asmx/TextSearch?text=West+bar&maxResults=100

因此,在您的情况下,您必须替换字符串“searchWord”的空格。有点像

 searchWord = searchWord.replace(" ", "+");

关于java - Android 中的 Saxparser 异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9857288/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com