首页

java读写xml文件 示例源码(Dom4j)

java

2020-6-30

import org.apache.commons.lang3.StringUtils;
import org.dom4j.*;
import org.dom4j.io.SAXReader;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

/**
 * 测试类: 测试dom4j相关api功能
 *
 * @author chenx
 * @date 2017/12/5
 */
@RunWith(JUnit4.class)
public class Dom4jTest {
    private Document document;

    @Before
    public void init() throws DocumentException {
        SAXReader saxReader = new SAXReader();
        document = saxReader.read(getClass().getResourceAsStream("Gps_Stops.xml"));
    }

    @Test
    public void testIterator() {
        Element root = document.getRootElement();
        for (Iterator<Element> iterator = root.elementIterator(); iterator.hasNext();) {
            Element element = iterator.next();
            if (element.isTextOnly()) {
                System.out.println(element.getQualifiedName()   ":"   element.getTextTrim());
            }
        }
    }

    @Test
    public void testXpath() {
        List<Node> nodes = document.selectNodes("//G_CONSIDERED_DATE");
        for (Node node : nodes) {
            Element element = (Element) node;
            Element curDate = element.element("CONSIDERED_DATE");
            System.out.println(curDate.getQualifiedName()   ":"   curDate.getTextTrim());
        }
    }

    @Test
    public void testIteratorAttr() {
        Map<String, String> namespaceMap = new HashMap<String, String>();
        namespaceMap.put("ns", "GpsStopsByGroupReport");
        XPath xPath = document.createXPath("//ns:table1_GroupName/ns:table1_UserNum_Collection/ns:table1_UserNum");
        xPath.setNamespaceURIs(namespaceMap);
        List<Node> nodes = xPath.selectNodes(document);
        for(int i=0;i<nodes.size();i  ){
            Node node=nodes.get(i);
            System.out.println(node.valueOf("@UserName"));

//            XPath otherXPath = DocumentHelper.createXPath("//ns:table1_GroupName/ns:table1_UserNum_Collection/ns:table1_UserNum");
//            otherXPath.setNamespaceURIs(namespaceMap);
//            List<Node> carsInfos = otherXPath.selectNodes(node);
//            carsInfos.forEach(carsInfo -> {
//                String interval = StringUtils.normalizeSpace(carsInfo.valueOf("@UserName"));
//                System.out.println(interval);
//            });

        }
    }

    @Test
    public void testIteratorByElement() {
        Element root = document.getRootElement();

        Iterator<Element> hotelInfos = root.elements().get(1).elements().get(0).elements().get(0).elements().get(0).elements().iterator();
        while (hotelInfos.hasNext()) {
            Element hotelInfo = hotelInfos.next();
            System.out.println(hotelInfo.attributeValue("Group"));

            Iterator<Element> carInfos = hotelInfo.elements().get(0).elements().iterator();
            while (carInfos.hasNext()) {
                Element carInfo = carInfos.next();
                System.out.println(carInfo.attributeValue("UserName"));
            }
        }
    }
}

资源下载此资源下载价格为3D币(VIP免费),请先
资源文件列表
.idea/compiler.xml , 632
.idea/libraries/Maven__jaxen_jaxen_1_1_6.xml , 462
.idea/libraries/Maven__junit_junit_4_12.xml , 455
.idea/libraries/Maven__org_apache_commons_commons_lang3_3_4.xml , 556
.idea/libraries/Maven__org_dom4j_dom4j_2_0_2.xml , 478
.idea/libraries/Maven__org_hamcrest_hamcrest_core_1_3.xml , 532
.idea/misc.xml , 443
.idea/modules.xml , 260
.idea/workspace.xml , 24125
dom4j-demo.iml , 1222
pom.xml , 1004
src/test/java/Dom4jTest.java , 3140
src/test/resources/Gps_Stops.xml , 42753
src/test/resources/history_forecast.xml , 8764
target/test-classes/Dom4jTest.class , 3996
target/test-classes/Gps_Stops.xml , 42753
target/test-classes/history_forecast.xml , 8764
没有账号? 忘记密码?

社交账号快速登录