dom4j(5) 编码 命名空间处理

来源:互联网 发布:linux ftp绑定域名 编辑:程序博客网 时间:2024/05/22 12:35

//设置命名空间

Map<String, String> namespaceURIs = new HashMap<String, String>();

namespaceURIs.put("xmlns", "http://www.test.com/test");

SAXReader reader = new SAXReader();

reader.getDocumentFactory().setXPathNamespaceURIs(namespaceURIs);

Document doc = reader.read(new File(file));


//解析时候再上命名空间

List<Node> list = doc.selectNodes("//xmlns:book");


//输出文件编码指定

OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("utf-8");

XMLWriter writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream(new File(dest)), "utf-8"), format);
writer.write(newDoc);
writer.close();

0 0
原创粉丝点击