XML文件处理中增加xmlns问题

07-05-17 smartlylife

我想生成一个<xml xmlns="http://www.test.net/test/" what="ttttt"><test>adfasdf</test></xml>这样形式的XML,但是使用addNamespace("", "XXXX")总是会在<test>里增加了一个xmlns="",如果加上前缀<test>里就不会增加xmlns="",这是为什么,如何能生成我想样的样式?3X

程序如下:

public static void main(String[] args) throws IOException {

Document document = DocumentHelper.createDocument();

Element root = document.addElement("xml");

String nameSpance = "http://www.test.net/test/";

root.addNamespace("", nameSpance);

Element test = root.addElement("test");

root.addAttribute("what", "ttttt");

test.setText("adfasdf");

StringWriter sw = new StringWriter();

XMLWriter xw = new XMLWriter(sw);

xw.write(document);

xw.flush();

xw.close();

System.out.println(sw.toString());

}

成生的结果如下:

<?xml version="1.0" encoding="UTF-8"?>

<xml xmlns="http://www.test.net/test/" what="ttttt"><test xmlns="">adfasdf</test></xml>