dom4j解析xml文件案例

来源:互联网 发布:recyclerview的优化 编辑:程序博客网 时间:2024/06/14 09:04

测试xml

<?xml version="1.0" encoding="UTF-8"?><xml-body>     <class name="bean.Userinfo" table="USERINFO" schema="SCOTT">        <aaa>aaaaaaaaa</aaa>        <id name="userid" type="int">            <column name="USERID" precision="22" scale="0"/>            <generator class="increment"/>        </id>        <property name="username" type="java.lang.String">            <column name="USERNAME" not-null="true"/>        </property>        <property name="password" type="java.lang.String">            <column name="PASSWORD" not-null="true"/>        </property>    </class><sex name="sunhonghua">nan</sex><sex name="sunhonghua">nan</sex></xml-body>

dom4j增删改查

public class ParseXml {public static void main(String[] args) throws Exception {// TODO Auto-generated method stubSAXReader reader = new SAXReader();Document document = reader.read(new File("src\\com\\huawei\\test\\user.xml"));// 注意文件路径从src起 否则不能读取文件addXml(document);// upateXml(document);// deleteXml(document);// readXml(document);}public static void ouputXml(Document document) throws IOException {// 文档中含有中文,设置编码格式写入的形式 无中文时不需要设置编码格式OutputFormat format = OutputFormat.createPrettyPrint();format.setEncoding("UTF-8");XMLWriter writer = new XMLWriter(new FileWriter("src\\com\\huawei\\test\\user.xml"));writer.write(document);writer.close();}public static void addXml(Document document) throws IOException {Element element = document.getRootElement();Element sexElm = element.addElement("sex");// 在某个节点下添加子节点Element aaa = DocumentHelper.createElement("aaa");// 直接创建某个节点// 还没设置成具体所属节点aaa.setText("aaaaaaaaa");List classlist = element.element("class").elements();classlist.add(0, aaa);// 添加为class的第一个节点 index=1时,第二个节点sexElm.setText("nan");sexElm.addAttribute("name", "sunhonghua");ouputXml(document);}public static void upateXml(Document document) throws IOException {Element element = document.getRootElement();Element sexElm = element.element("sex");Attribute attribute = sexElm.attribute("name");attribute.setValue("11111122222");// setValue setText 均是改变sex的name 的属性值// attribute.setText("11111111"); 也可以用来设置Element的文本值ouputXml(document);}public static void deleteXml(Document document) throws IOException {Element element = document.getRootElement();Element sexElm = element.element("sex");element.remove(sexElm);ouputXml(document);}public static void readXml(Document document) throws IOException {Element element = document.getRootElement();// <xml-body>为根元素Element classelement = element.element("class");List listElm = classelement.elements();Iterator iterator = listElm.iterator();while (iterator.hasNext()) {Element elementNode = (Element) iterator.next();System.out.println(elementNode.getName());}// ouputXml(document);}public static void searchXml(Document document) throws IOException {Element element = document.getRootElement();// <xml-body>为根元素Element classelement = element.element("class");List listElm = classelement.elements();Iterator iterator = listElm.iterator();while (iterator.hasNext()) {Element elementNode = (Element) iterator.next();System.out.println(elementNode.getName());}// ouputXml(document);}}



0 0
原创粉丝点击