加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

当xml中存在命名空间,dom4j解析以及写入xml文档时的乱码问题

发布时间:2020-12-16 09:21:19 所属栏目:百科 来源:网络整理
导读:最近公司项目开发中需要通过前台用户界面进行客户业务系统的部署(提供界面化操作,减少运维工作的难度),通过修改web.xml进行设置各个项目不同的信息配置。 开发过程中遇到2种问题,同时将解决方案备注上,以方便日后查看。 问题一: 当xml中存在命名空间,三

最近公司项目开发中需要通过前台用户界面进行客户业务系统的部署(提供界面化操作,减少运维工作的难度),通过修改web.xml进行设置各个项目不同的信息配置。

开发过程中遇到2种问题,同时将解决方案备注上,以方便日后查看。

问题一:当xml中存在命名空间,三种处理办法(dom4j)

问题二:文件保存之后总是提示中文乱码问题

针对上面2个问题的解决方案进行汇总,解决方法主要还是来自于其他网络同行的博客。

第一个 问题主要参照 博客http://blog.sina.com.cn/s/blog_5cef6d430100krt1.html

xml代码example:


<reportxmlns="http://www.eclipse.org/birt/2005/design" version="3.2.15" id="1">
<list-property name="cssStyleSheets">
<structure>
<property name="fileName">D: eport.css</property>
</structure>
</list-property>
</report>


第一个方案.设置你的xpath的命名空间setNamespaceURIs

public class TransferXML {
public static void main(String[] args) throws Exception{
Map map = new HashMap();
map.put("design","http://www.eclipse.org/birt/2005/design"); // 加入命名空间
SAXReader saxReader = new SAXReader();
File file = new File("D:test.xml");
Document document = saxReader.read(file);
XPath x = document.createXPath("//design:list-property"); //创建list-property查询
x.setNamespaceURIs(map); // 设置命名空间
List nodelist = x.selectNodes(document); //获取文档下所有list-property节点
System.out.println(nodelist.size());
}
}


第二个解决方案:设置你的DocumentFactory()的命名空间 setXPathNamespaceURIs

http://www.eclipse.org/birt/2005/design");
SAXReader saxReader = new SAXReader();
File file = new File("D:test.xml");
saxReader.getDocumentFactory().setXPathNamespaceURIs(map);
Document document = saxReader.read(file);
List tmp = document.selectNodes("//design:list-property");
System.out.println(tmp.size());
}
}


第三种方法:最笨也是最通用的方法,就是不使用开发环境给你提供的一系列对象,而是用XPath语法中自带的local-name() 和 namespace-uri() 指定你要使用的节点名和命名空间。
当你遇到使用xslt来样式化xml时,就知道这个笨方法的好处了:
public class TransferXML {
public static void main(String[] args) throws Exception
SAXReader saxReader = new SAXReader();
File file = new File("D:test.xml");
Document document = saxReader.read(file);
List tmp = document.selectNodes("//*[local-name()='report' and namespace-uri()='http://www.eclipse.org/birt/2005/design']/* [local-name()='list-property']");
System.out.println(tmp.size());
}
}

第二个 问题主要参照:http://www.52php.cn/article/p-tyogsawy-bgq.html

book.xml

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

<书架>
<书>
<书名>Java就业基础教程</书名>
<作者>张孝祥</作者>
<售价>48元</售价>
<售价>58元</售价>
</书>
<书>
<书名>JavaScript网页开发</书名>
<作者>黎活明</作者>
<售价>46元</售价>
</书>
</书架>

Dom4j.java

package yyy.dom4j;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.util.List;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import org.junit.Test;

public class Dom4j {

private File file = new File("src/book.xml");
//遍历xml文档
@Test
public void testList() throws Exception{
SAXReader reader = new SAXReader();
Document document = reader.read(file);
Element root = document.getRootElement();
list(root);
}
public void list(Element element){
System.out.println(element.getName());
List<Node> nodes = element.elements();
//System.out.println(nodes.size());
for(Node node : nodes){
if(node instanceof Element){
list((Element) node);
}
}
}

//读取某个节点的值
@Test
public void read() throws Exception{
SAXReader reader = new SAXReader();
Document document = reader.read(file);
Element root = document.getRootElement();
Element price = root.element("书").element("作者");
String value = price.getText();
System.out.println(value);
}

//添加某个节点
@Test
public void add() throws Exception{
SAXReader reader = new SAXReader();
Document document = reader.read(file);

Element price = DocumentHelper.createElement("售价"); //创建新的节点
price.setText("44元"); //设置节点值

Element root = document.getRootElement();
root.element("书").add(price); //添加节点

//将内存中的document对象重新写入xml文档 特别要注意乱码问题

//方法1:使用OutputStreamWriter设置写入文档时所使用的编码表OutputStreamWriter out = new OutputStreamWriter(new FileOutputStream(file),"utf-8");document.write(out);out.close();/*//方法2:OutputFormat format = OutputFormat.createPrettyPrint();format.setEncoding("gb2312");XMLWriter writer = new XMLWriter(new FileWriter(file),format);writer.write(document);writer.close();*/ /*//方法3:OutputFormat format = OutputFormat.createPrettyPrint();XMLWriter writer = new XMLWriter(new OutputStreamWriter(new FileOutputStream(file),"utf-8"),format);writer.write(document);writer.close();*//*//方法4:OutputFormat format = OutputFormat.createPrettyPrint();format.setEncoding("gb2312"); //源文档用"gb2312"编码,既不改变源文档的编码格式,又不能有乱码XMLWriter writer = new XMLWriter(new FileOutputStream(file),format);writer.write(document);writer.close();*//*//document对象在内存中是以"UTF-8"编码形式存在,用FileWriter将document对象以字符流的形式写入xml文档默认是用本地码表"gb2312"编码 * 乱码问题总结: * 1.用字符流向文件写入数据要考虑乱码问题,而用字节流就不必考虑乱码问题 * 用字符流向文件写入数据默认使用本地码表即"gb2312" * 2.任何对象读入内存都是以"UTF-8"编码的形式存在 * 默认情况下XMLWriter的write方法是以"UTF-8"的编码形式将内存中的document对象传给FileWriter,所以要想不发生乱码问题, * 就要使用包装流OutputStreamWriter并给定写入文件时所使用的编码表,或者使用OutputFormat的setEncoding方法指定传给 * 流对象时所使用的编码格式。 * */}//向指定位置添加节点@Testpublic void add2() throws Exception{SAXReader reader = new SAXReader();Document document = reader.read(file);Element root = document.getRootElement();Element price = DocumentHelper.createElement("售价");price.setText("48元");List list = root.element("书").elements();list.add(2,price);OutputFormat format = OutputFormat.createPrettyPrint();XMLWriter writer = new XMLWriter(new FileOutputStream(file),format);writer.write(document);writer.close();}//删除某个节点@Testpublic void delete() throws Exception{SAXReader reader = new SAXReader();Document document = reader.read(file);Element root = document.getRootElement();Element price = root.element("书").element("售价");price.getParent().remove(price);OutputFormat format = OutputFormat.createPrettyPrint();XMLWriter writer = new XMLWriter(new FileOutputStream(file),format);writer.write(document);writer.close();}//更改某个节点的内容@Testpublic void update() throws Exception{SAXReader reader = new SAXReader();Document document = reader.read(file);Element root = document.getRootElement();Element price = root.element("书").element("售价");price.setText("58元");OutputFormat format = OutputFormat.createPrettyPrint();XMLWriter writer = new XMLWriter(new FileOutputStream(file),format);writer.write(document);writer.close();}}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读