“java org.w3c.dom.domexception invalid_character_err指定了无效或非法的xml字符”

时间:2014-03-25 10:35:17

标签: java xml

当我运行以下代码时,会发生异常" java org.w3c.dom.domexception invalid_character_err指定了无效或非法的xml字符"。

请帮帮我

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                DocumentBuilder builder=factory.newDocumentBuilder();
                org.w3c.dom.Document document = builder.parse("F:/shared/Manideep messg.xml");
                ((org.w3c.dom.Document) document).getDocumentElement().normalize();
                Element root = document.getDocumentElement();
                 Collection<Chat> ch = new ArrayList<Chat>();
                 ch.add(new Chat());

                 System.out.println(document.getElementsByTagName("chat"));

//creating user message tag
                 String conct = fromUser+"-"+toUser;
                 String[] split = conct.split(" ");

                 for (int i = split.length - 1; i >= 0; i--)
                 {
                 usersTag += (split[i] + " ");
                 }

                msgTag=usersTag.trim();


                  org.w3c.dom.Element name=null;
                 if(document.getElementsByTagName(msgTag).getLength()==0)
                 {
                     System.out.println("in if condiotion");
                      name = (org.w3c.dom.Element) ((org.w3c.dom.Document) document).createElement(msgTag);
                        // ((Node) name).appendChild(((org.w3c.dom.Document) document).createTextNode(i.getMessage()));


     ((Node) root).appendChild((Node) name);
                         System.out.println("--------end of if");
                 }
                 NodeList list = document.getElementsByTagName(msgTag);
                 name= (Element) list.item(0);
                 System.out.println("Taag : "+name);

1 个答案:

答案 0 :(得分:0)

执行在Java 8运行时下使用Java 7编译的代码时遇到此错误。使用Java 7执行旧的二进制文件或重建然后使用Java 8执行都可以避免错误。