(XPath、XMl和对象之间的转换)
1、XPath的介绍
XPath 是一门在 XML 文档中查找信息的语言。类似于数据库的sql。
XPath 是通过元素和属性进行查找
XPath简化了Dom4j查找节点的过程
使用XPath必须导入jaxen-1.1-beta-6.jar
否则出现NoClassDefFoundError: org/jaxen/JaxenException
2、Xpath语法:
package com.test; import java.io.File; import java.util.List; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.io.SAXReader; public class TestXPath { public static void main(String[] args) { File xmlFile = new File( "D:\\WorkSpace6.5\\java1603\\Eclipse\\Xml_Project\\src\\student.xml"); SAXReader saxReader = new SAXReader(); String xpath = ""; List<Element> stuList = null; Element element = null; try { Document document = saxReader.read(xmlFile); Element root = document.getRootElement(); /** * 1:通过XPath来查找节点 */ // 1:查找XML所有的student xpath = "/root/student"; stuList = document.selectNodes(xpath); System.out.println(stuList.size()); /** * 2:使用相对路径,从root来查询 */ stuList = root.selectNodes("student"); System.out.println(stuList.size()); /** * 3:获取所有name为student元素对象,不考虑位置 */ stuList = document.selectNodes("//student"); System.out.println(stuList.size()); // 2:查询sudent元素中id = 4的元素节点 element = (Element) document.selectSingleNode("//student[@id='4']"); System.out.println(element.attributeValue("id")); // 3:查询性别为男的元素节点 stuList = document.selectNodes("/root/student[stu_sex='男']"); System.out.println(stuList.size()); // 4:查询名称中包含"a"的元素节点,类似like语句。 stuList = document .selectNodes("/root/student[contains(stu_name,'a')]"); System.out.println(stuList.size()); // 4:查询名称中包含"a"的元素节点,或者年龄大于20岁 stuList = document .selectNodes("/root/student[contains(stu_name,'a') or stu_age>=20]"); System.out.println(stuList.size()); // 4:查询名称中包含"a"的元素节点,并且年龄大于20岁 stuList = document .selectNodes("/root/student[contains(stu_name,'a')][stu_age>=20]"); System.out.println(stuList.size()); } catch (Exception e) { e.printStackTrace(); } } }4、XMl与对象之间的转换
package com.test; import java.io.FileOutputStream; import com.bean.StudentBean; import com.thoughtworks.xstream.XStream; import com.thoughtworks.xstream.io.xml.DomDriver; import com.thoughtworks.xstream.io.xml.XmlFriendlyNameCoder; import com.thoughtworks.xstream.io.xml.XppDriver; /** * XML与Java对象之间的转换 * * @author ctd * */ public class XmlToJava { public static void main(String[] args) { XmlToJava.objectToXML(); // XmlToJava.xmlToObject(); } private static void objectToXML() { StudentBean student = new StudentBean(); student.setStu_id(1); student.setStu_name("admin"); student.setStu_sex("男"); student.setStu_content("备注内容"); student.setStu_age(20); /** * 将对象转为XML字符串/XML文件 * * 1:通过字符串的拼接 * * 2:通过X-Stream对象来进行转换,得包含xstream-1.4.5.jar */ /** * 解决下划线的问题 */ XStream stream = new XStream(new XppDriver(new XmlFriendlyNameCoder( "_-","_"))); /** * 更改根节点的名称 */ stream.alias("root",StudentBean.class); /** * 将id做为属性 */ stream.aliasAttribute(StudentBean.class,"stu_id","id"); System.out.println(stream.toXML(student)); /** * 生成一个文件 */ try { FileOutputStream fileOutputStream = new FileOutputStream( "E:\\test.xml"); fileOutputStream.write("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" .getBytes()); fileOutputStream.write("\n".getBytes()); stream.toXML(student,fileOutputStream); } catch (Exception e) { e.printStackTrace(); } } private static void xmlToObject() { XStream stream = new XStream(new DomDriver()); stream.alias("root",StudentBean.class); stream.aliasAttribute(StudentBean.class,"id"); StringBuffer xml = new StringBuffer(); xml.append("<root id='2'>"); xml.append(" <stu_name>admin</stu_name>"); xml.append(" <stu_sex>男</stu_sex>"); xml.append(" <stu_age>20</stu_age>"); xml.append(" <stu_content>备注内容</stu_content>"); xml.append("</root>"); StudentBean studentBean = (StudentBean) stream.fromXML(xml.toString()); System.out.println(studentBean.getStu_id() + "\t" + studentBean.getStu_name()); } }studentBean.java
package com.bean; public class StudentBean { private int stu_id; private String stu_name; private String stu_sex; private int stu_age; private String stu_content; public int getStu_id() { return stu_id; } public void setStu_id(int stu_id) { this.stu_id = stu_id; } public String getStu_name() { return stu_name; } public void setStu_name(String stu_name) { this.stu_name = stu_name; } public String getStu_sex() { return stu_sex; } public void setStu_sex(String stu_sex) { this.stu_sex = stu_sex; } public int getStu_age() { return stu_age; } public void setStu_age(int stu_age) { this.stu_age = stu_age; } public String getStu_content() { return stu_content; } public void setStu_content(String stu_content) { this.stu_content = stu_content; } }