标签:exception ips clip com src file dom nts sax
package com.cj.DOM4J;
import java.io.File;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import org.junit.Test;
import org.w3c.dom.NodeList;
public class TestDom4J {
@Test
public void test111() throws Exception{
SAXReader reader =new SAXReader();
Document doc = reader.read(new File("G:\\EclipseWorkSpace\\testXml\\src\\com\\cj\\DOM4J\\contacts.xml"));
System.out.println(doc);
}
//取出第二个联系人的名字
@Test
public void getName() throws Exception{
/*
* 1.先得到根(contacts) -- doc.getRootElement();
2.在根下面的某一个节点 -- root.elements(“linkman”)
3.确定拿到某个linkman.elementText(“name”)
*
* */
SAXReader reader =new SAXReader();
Document doc = reader.read(new File("G:\\EclipseWorkSpace\\testXml\\src\\com\\cj\\DOM4J\\contacts.xml"));
Element contact = doc.getRootElement();
List<Element> linkmans = contact.elements("linkman");
String value = linkmans.get(1).elementText("name");
System.out.println(value);
}
}
标签:exception ips clip com src file dom nts sax
原文地址:https://www.cnblogs.com/wzscom/p/10289222.html