虽然我运行下面的代码,但会发生异常“java org.w3c.dom.domexception invalid_character_err指定了无效或非法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 ch = new ArrayList();
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
错误很明显:您打开的XML文档格式不正确。 –
+0
谢谢Chrylis。这里是我的XML文件。 喜您好 WHR - [R U&in U.S. hw r u?罚款.. –
+0
这对我来说很好。你的代码在哪里失败?它是在解析你的文档,还是在以后建立新节点的时候?你可以发布堆栈跟踪和行号信息来匹配代码片段吗? –