在生成XML时无法使用特殊的字符“ - ”

时间:2016-02-22 13:04:06

标签: java xml

我使用JAVA构建XML,我的元素具有很少的属性,该属性包含' - '

   but when setting attibute as :

  DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  DocumentBuilder builder        = factory.newDocumentBuilder();
  Document doc                   = builder.newDocument();
  Element dffgr=doc.createElement("diffgr:diffgram");
  dffgr.setAttribute("xmlns:msdata", "urn:schemas-­microsoft­-com:xml­-msdata".toString());
  dffgr.setAttribute("xmlns:diffgr", "urn:schemas-­microsoft­-com:xml­diffgram-­v1".toString());



'-' is replaced by 'xAD'
as  Output is :
<diffgr:diffgram xmlns:diffgr="urn:schemasέicrosoftΣom:xmlΤiffgramζ1" xmlns:msdata="urn:schemasέicrosoftΣom:xmlέsdata">


and desired output is :
<diffgr:diffgram xmlns:msdata="urn:schemas­microsoft­com:xml­msdata" xmlns:diffgr="urn:schemas­microsoft­com:xml­diffgram­v1">

请帮助。

1 个答案:

答案 0 :(得分:0)

复制并粘贴此内容:

dffgr.setAttribute("xmlns:msdata", "urn:schemas-microsoft­-com:xml­-msdata");
dffgr.setAttribute("xmlns:diffgr", "urn:schemas-microsoft­-com:xml­diffgram-v1");

-使用了错误的字符。