请教使用JDOM解析w3c document出错!
package com.self.test;
import java.io.StringReader;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.jdom.input.DOMBuilder;
import org.w3c.dom.Document;
import org.xml.sax.InputSource;
public class JdomTest {
public JdomTest(){
}
public void publish(){
DocumentBuilder docBuilder;
Document statusDoc = null;
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
docBuilderFactory.setIgnoringElementContentWhitespace(true);
try {
docBuilder = docBuilderFactory.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(getOMAPartialStatusXML("1", "sip:aaa@micromethod.com")));
statusDoc = docBuilder.parse(is);
DOMBuilder builder = new DOMBuilder();
org.jdom.Document doc = builder.build(statusDoc);
}
catch (Exception e) {
System.out.println("Failed to parse the xml: " + e.getMessage());
}
}
private String getOMAPartialStatusXML(String newStatus, String sipURI) {
String xmlData =
"<?xml version=\"1.0\" encoding=\"UTF-8\"?>" +
"<pdiff:pidf-diff xmlns=\"urn:ietf:params:xml:ns:pidf\" " + //如果在xmlns后面加个冒号":"和随便一个名字就能正常运行!
"xmlns:pdiff=\"urn:ietf:params:xml:ns:pidf-diff\" " +
"xmlns:pdm=\"urn:ietf:params:xml:ns:pidf:data-model\" " +
"xmlns:rpid=\"urn:ietf:params:xml:ns:pidf:rpid\" " +
"xmlns:op=\"urn:oma:xml:prs:pidf:oma-pres\" " +
"xmlns:lt=\"urn:ietf:params:xml:ns:location-type\" " +
"entity=\"" + sipURI + "\"> " +
"<pdiff:replace sel=\"*/pdm:person/rpid:mood/rpid:amazed\"> " +
"<rpid:angry/></pdiff:replace> " +
"</pdiff:pidf-diff>";
return xmlData;
}
public static void main(String[] args) throws Exception{
new JdomTest().publish();
}
}
报如下异常:
org.jdom.IllegalAddException: The namespace xmlns="urn:ietf:params:xml:ns:pidf" could not be added as a namespace to "pidf-diff": The namespace prefix "" collides with the element namespace prefix
at org.jdom.Element.addNamespaceDeclaration(Element.java:341)
at org.jdom.DefaultJDOMFactory.addNamespaceDeclaration(DefaultJDOMFactory.java:189)
at org.jdom.input.DOMBuilder.buildTree(DOMBuilder.java:244)
at org.jdom.input.DOMBuilder.buildTree(DOMBuilder.java:181)
at org.jdom.input.DOMBuilder.build(DOMBuilder.java:144)
at com.self.test.JdomTest.publish(JdomTest.java:28)
at com.self.test.JdomTest.main(JdomTest.java:54)
但我如果把xml字符串改为:
<pdiff:pidf-diff xmlns:aaa=\"urn:ietf:params:xml:ns:pidf\"
后可以正确执行!请问这是什么原因???
谢谢!
[解决办法]
JDOM' bug