0

When I am trying to read xml file from my Java code, I getting error as shown in attached image. enter image description here

Java code :

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));
 }

When I am running the same application using main class, it is working fine. But when I am trying to do the same using struts action, I am getting this error. Please help me out.

4

2 回答 2

0

尝试使用阅读器来解析和指定字符集:

Document document = builder.parse(new InputStreamReader(new FileInputStream(file), Charset.forName("UTF-8"));

我也同意另一个答案,即您可能错误地引入了旧的/不同的 xml 解析器。

于 2013-03-22T13:56:47.030 回答
0

堆栈跟踪表明您正在使用 xerces 作为您的 xml 解析器(而不是作为 jre/jdk 一部分的 xml 解析器)。使用不同的 xml 解析器可能会导致各种问题。除非您是故意这样做的,否则我建议您将其删除。(如果你使用像 maven 这样的构建系统,有时你会得到对“xercesImpl”或 xml-apis“jars 的传递依赖。你应该在你的 pom 中排除这些。)

于 2013-03-22T13:50:11.780 回答