xml解析问题!急

lyp_2008001 2009-11-17 05:58:43
我是利用axiom对xml进行解析!
但却出现下面的错误!
Exception in thread "main" org.apache.axiom.om.OMException: [com.ctc.wstx.exc.WstxLazyException] com.ctc.wstx.exc.WstxUnexpectedCharException: Unexpected character ' ' (code 32) (missing name?)
at [row,col {unknown-source}]: [136,53]
程序在执行有些xml文件时没有问题,有些却有问题。我很苦恼!是不是我的xml文件有问题啊?

public class AxiomDemo {

public static void writeToFile(OutputStream os, byte[] b) {//写内容写到文件里面

try {
os.write(b, 0, b.length);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public static void writeToFile(OutputStream os, OMElement om) {
try {
String str = om.toStringWithConsume();
byte[] b = str.getBytes();
//os.write(b, 0, b.length);
writeToFile(os, b);
} catch (XMLStreamException e) {
// TODO: handle exception
}
}

public static OMElement readQuestionXml(InputStream is) {
OMElement om = null;
try {
XMLStreamReader parser = XMLInputFactory.newInstance()
.createXMLStreamReader(is);
StAXOMBuilder builder = new StAXOMBuilder(parser);
// get the root element
om = builder.getDocumentElement();
} catch (XMLStreamException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (FactoryConfigurationError e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return om;
}

public static void handlerQuestionXml(OutputStream os, OMElement om)
throws OMException, XMLStreamException {
Iterator iter = om.getChildElements();
long start = System.currentTimeMillis();
while (iter.hasNext()){
OMElement omquestion = (OMElement)iter.next();
QName q_content = new QName("qcontent");
QName q_answers = new QName("answers");
QName q_answer = new QName("answer");
QName q_acontent = new QName("acontent");
OMElement om_qContent = omquestion.getFirstChildWithName(q_content);
OMElement om_acontent = omquestion.getFirstChildWithName(q_answers).getFirstChildWithName(q_answer).getFirstChildWithName(q_acontent);
String content = om_qContent.getText();
//System.out.println(content);
String acontent = om_acontent.getText();
//&& acontent.length() <=20
if(content.length() <=15 && acontent.length() <=20){
//writeToFile(os, omquestion);
String question = omquestion.toStringWithConsume()+"\r\n";
writeToFile(os, question.getBytes());
} //System.out.println(omquestion.toString());
}
long end = System.currentTimeMillis();
System.out.println("title time:"+(end-start));
}

public static void main(String[] args) {
String filePath = "D:/QuanTa/AxiomDemo/xmlfile/1.xml";
String outFilePath = "D:/question2.xml";
try {
InputStream is = new FileInputStream(new File(filePath));
OutputStream os = new FileOutputStream(new File(outFilePath));
OMElement om = readQuestionXml(is);
System.out.println(om);
handlerQuestionXml(os, om);
is.close();
os.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
// TODO: handle exception
System.out.println(e.getMessage());
} catch (XMLStreamException e) {
// TODO: handle exception
}

}


}
...全文
613 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
lyp_2008001 2009-11-18
  • 打赏
  • 举报
回复
顶上去!
bossonljt 2009-11-17
  • 打赏
  • 举报
回复
路过
lyp_2008001 2009-11-17
  • 打赏
  • 举报
回复
用正则表达式如何进行xml的解析,有没有源码参考一下?谢谢!
designedIt 2009-11-17
  • 打赏
  • 举报
回复
路过---》跳过
designedIt 2009-11-17
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 lyp_2008001 的回复:]
恩,很有可能。那有什么方式可以避免吗?因为xml文件是别人的不能够修改。能不能通过程序避免呢?谢谢
[/Quote]
若XML格式不正规,XML解析器是会报错。你可通过捕获错误来路过该XML文件的处理。(因为若XML文件格式不正规,无法再往下处理了,除非你自己写一个能处理格式不正规的XML的解析器--或你的解析器有选项支持格式不正规的处理)
lyp_2008001 2009-11-17
  • 打赏
  • 举报
回复
恩,很有可能。那有什么方式可以避免吗?因为xml文件是别人的不能够修改。能不能通过程序避免呢?谢谢
designedIt 2009-11-17
  • 打赏
  • 举报
回复
是不是我的xml文件有问题啊?
是的。错误信息表明你的XML文件是"not well-formed"(非格式正规的)。

62,621

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧