Java 无法读取XML文件
当我试图从我的Java代码中读取xml文件时,我发现了错误,如附图所示。 Java代码:Java 无法读取XML文件,java,xml,struts,Java,Xml,Struts,当我试图从我的Java代码中读取xml文件时,我发现了错误,如附图所示。 Java代码: public ActionForward xmlupload(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { String
public ActionForward xmlupload(ActionMapping mapping,
ActionForm form, HttpServletRequest request,
HttpServletResponse response) throws IOException, ServletException {
String target = new String("success");
System.out.println("I AM IN ACTION");
List<XmltodbBO> branchList = new ArrayList<XmltodbBO>();
try {
File file = new File("D:\\FNDWRR.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(file);
doc.getDocumentElement().normalize();
System.out.println("Root element " + doc.getDocumentElement().getNodeName());
NodeList nodeLst = doc.getElementsByTagName("fsg:RptLine");
System.out.println("Root Tag value");
XmltodbDAO sim=new XmltodbDAO();
for (int s = 0; s < nodeLst.getLength(); s++) {
XmltodbBO bO=new XmltodbBO();
Node fstNode = nodeLst.item(s);
if (fstNode.getNodeType() == Node.ELEMENT_NODE) {
Element fstElmnt = (Element) fstNode;
NodeList fstNmElmntLst = fstElmnt.getElementsByTagName("fsg:RptCell");
System.out.print(fstNmElmntLst.getLength());
Node current = fstNmElmntLst.item(0);
if(current.getNodeType() == Node.ELEMENT_NODE){
String nodeName = current.getNodeName();
Element fstNmElmnt = (Element) fstNmElmntLst.item(0);
NodeList fstNm = fstNmElmnt.getChildNodes();
Text itemCheckedText = (Text) fstNm.item(0);
if (itemCheckedText != null) {
System.out.println("Tag value : " + ((Node) fstNm.item(0)).getNodeValue());
bO.setFirstfieldName( fstNm.item(0).getNodeValue());
}
}
if( fstNmElmntLst.getLength()>1){
Node current1 = fstNmElmntLst.item(1);
if(current1.getNodeType() == Node.ELEMENT_NODE){
Element fstNmElmnt = (Element) fstNmElmntLst.item(1);
NodeList fstNm = fstNmElmnt.getChildNodes();
Text itemCheckedText1 = (Text) fstNm.item(0);
if (itemCheckedText1 != null) {
System.out.println("Tag value : " + ((Node) fstNm.item(0)).getNodeValue());
bO.setSecondfieldName( fstNm.item(0).getNodeValue());
}
}
}
if( fstNmElmntLst.getLength()>2){
Node current1 = fstNmElmntLst.item(2);
if(current1.getNodeType() == Node.ELEMENT_NODE){
Element fstNmElmnt = (Element) fstNmElmntLst.item(2);
NodeList fstNm = fstNmElmnt.getChildNodes();
Text itemCheckedText1 = (Text) fstNm.item(0);
if (itemCheckedText1 != null) {
System.out.println("Tag value : " + ((Node) fstNm.item(0)).getNodeValue());
bO.setThirdfieldName(fstNm.item(0).getNodeValue());
}
}
}
} branchList.add( bO); }
sim.adddb(branchList);
}
catch (Exception e) {
e.printStackTrace();
}
return (mapping.findForward(target));
}
公共ActionForward xmlupload(ActionMapping映射,
ActionForm表单,HttpServletRequest请求,
HttpServletResponse响应)引发IOException,ServletException{
字符串目标=新字符串(“成功”);
System.out.println(“我正在行动”);
List branchList=new ArrayList();
试一试{
File File=新文件(“D:\\FNDWRR.xml”);
DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
DocumentBuilder db=dbf.newDocumentBuilder();
文档doc=db.parse(文件);
doc.getDocumentElement().normalize();
System.out.println(“根元素”+doc.getDocumentElement().getNodeName());
NodeList nodeLst=doc.getElementsByTagName(“fsg:RptLine”);
System.out.println(“根标记值”);
XmltodbDAO sim=新XmltodbDAO();
对于(int s=0;s当我使用main类运行同一个应用程序时,它工作正常。但是当我尝试使用struts操作来做同样的事情时,我得到了这个错误。请帮帮我 堆栈跟踪表明您正在使用xerces作为xml解析器(与作为jre/jdk一部分的xml解析器相反)。使用不同的xml解析器可能会导致各种问题。除非你是故意这么做,否则我建议你把它去掉。(如果您使用的是maven之类的生成系统,有时您会获得对“xercesImpl”或xml API“jar”的可传递依赖项。您应该在pom中排除这些依赖项。)尝试使用读取器解析和指定字符集:
Document document = builder.parse(new InputStreamReader(new FileInputStream(file), Charset.forName("UTF-8"));
我也同意另一个答案,即您可能错误地引入了一个旧的/不同的xml解析器。您使用的是哪个字符集?错误表明它在xml上发现了一个不寻常的字符…您可以发布它吗?尝试使用IE打开xml,它将为您提供无效字符的位置。我强烈不同意这个建议。xmlJDK中的解析器完全有缺陷,Apache Xerces one要好得多。@MichaelKay-好吧,JDK impl是基于Xerces的,尽管是旧版本。是的,内置版本肯定有问题。但是在处理xml时,如果出现问题,10次中有9次是由于意外地拉入xercesImpl有许多xml API(jaxp、jaxws、jaxb)在java和内置impls中,至少可以很好地相互配合。JDK中的bug不会影响10%的运行,当然。它们可能只影响5000次运行中的一次。这使得这些bug特别有害——在上线之前你不会发现它们。@MichaelKay-我的观点是,包括xercesImpl会导致更多的问题(显然)与使用jdk的内置解析器相比,问题更多,通常是由于与jdk中的其他xml API的不良交互“jdk xml解析器的问题。嗯,我的经验正好相反。我从来没有在Apache Xerces解析器上遇到过任何问题,我在jdk解析器上也遇到过很多问题。