无法解析数据,因为我收到 expatParser 异常
伙计们 以下是我试图解析的 xml
<?xml version="1.0" encoding="UTF-8"?><Categories><category name="Banquet & Marriage Hall" id="1" image=""/><category name="Crematorium, Burial Ground" id="2" image=""/><category name="Educational Institution" id="3" image=""/><category name="Embassies & Consulates" id="4" image=""/><category name="Fire Station" id="5" image=""/><category name="Government Office" id="6" image=""/></Categories>
以下是我正在使用 sp.parse() 的解析器的代码
public byte parse(){
SAXParserFactory spf = null;
SAXParser sp = null;
InputStream inputStream = null;
try {
inputStream = new ByteArrayInputStream(data.getBytes());
spf = SAXParserFactory.newInstance();
if (spf != null) {
sp = spf.newSAXParser();
**sp.parse(inputStream, this);**
}
}
/*
* Exceptions need to be handled MalformedURLException
* ParserConfigurationException IOException SAXException
*/
catch (Exception e) {
System.out.println("Exception: " + e);
e.printStackTrace();
} finally {
try {
if (inputStream != null)
inputStream.close();
} catch (Exception e) {
}
}
if (categorieslist != null && categorieslist.size() > 0) {
// Log.d("Array List Size",""+tipsList.get(4).getTitle());
return 1;
} else {
return 0;
}
}
public ArrayList<Categories> getParserCategoriesList(){
return categorieslist;
}
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
if(localName.equalsIgnoreCase("Categories")){
if(localName.equalsIgnoreCase("category")){
categories = new Categories();
categorieslist.add(categories);
categories.setId(attributes.getValue("id"));
Log.d("ID",attributes.getValue("id"));
categories.setName(attributes.getValue("name"));
Log.d("NAME",attributes.getValue("name"));
/*categories.setImage(attributes.getValue("image"));
Log.d("image",attributes.getValue("image"));*/
}
}
是给我 expatParser 异常 的代码 我在之前的 5 个 xml 解析中一直使用相同的逻辑,但没有收到此错误。 我做错了什么还是 xml 错误?
guys
Following is the xml which i am trying to parse
<?xml version="1.0" encoding="UTF-8"?><Categories><category name="Banquet & Marriage Hall" id="1" image=""/><category name="Crematorium, Burial Ground" id="2" image=""/><category name="Educational Institution" id="3" image=""/><category name="Embassies & Consulates" id="4" image=""/><category name="Fire Station" id="5" image=""/><category name="Government Office" id="6" image=""/></Categories>
Following is the code of my parser which i am using
public byte parse(){
SAXParserFactory spf = null;
SAXParser sp = null;
InputStream inputStream = null;
try {
inputStream = new ByteArrayInputStream(data.getBytes());
spf = SAXParserFactory.newInstance();
if (spf != null) {
sp = spf.newSAXParser();
**sp.parse(inputStream, this);**
}
}
/*
* Exceptions need to be handled MalformedURLException
* ParserConfigurationException IOException SAXException
*/
catch (Exception e) {
System.out.println("Exception: " + e);
e.printStackTrace();
} finally {
try {
if (inputStream != null)
inputStream.close();
} catch (Exception e) {
}
}
if (categorieslist != null && categorieslist.size() > 0) {
// Log.d("Array List Size",""+tipsList.get(4).getTitle());
return 1;
} else {
return 0;
}
}
public ArrayList<Categories> getParserCategoriesList(){
return categorieslist;
}
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
if(localName.equalsIgnoreCase("Categories")){
if(localName.equalsIgnoreCase("category")){
categories = new Categories();
categorieslist.add(categories);
categories.setId(attributes.getValue("id"));
Log.d("ID",attributes.getValue("id"));
categories.setName(attributes.getValue("name"));
Log.d("NAME",attributes.getValue("name"));
/*categories.setImage(attributes.getValue("image"));
Log.d("image",attributes.getValue("image"));*/
}
}
sp.parse() is the code which is giving me the expatParser Exception
I have been using the same logic in previous 5 xml parsing and i dont get this error.
What am i doing wrong or is it dat the xml is wrong ??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为解析器在到达与号(&)字符时抛出异常。您可以在此处找到与您的问题类似的问题
I think the parser is throwing the exception when it reaches the ampersand (&) character. You can find a problem similar to yours here
是的,前面的评论是正确的,解析器在到达 & 时抛出异常或任何特殊字符,有两个选项。
一切顺利
yes previous comment is right, parser is throwing the exception when it reaches & or any special character, two options are there.
all the best