本文主要是介绍XML-dom4j实战,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
dom4j是一个用来读取XML的工具包,它是采用DOM思想来读取的,也就是把XML组织成一个文档树,然后根据相应的节点来读取。
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.Node;
import org.dom4j.Text;
import org.dom4j.io.SAXReader;
import org.junit.Test;/*** 编写DOM4J的工具类* @author Neuclil**/public class ReadUtils {@Testpublic void test(){File file = new File("./src/contact.xml");List<Contact> contacts = getContacts(file);Iterator<Contact> iterator = contacts.iterator();while(iterator.hasNext()){Contact contact = iterator.next();System.out.println(contact);}}public static Document readFile(File file){SAXReader saxReader = new SAXReader();Document doc = null;try {doc = saxReader.read(file);} catch (DocumentException e) {e.printStackTrace();}return doc;}//演示读取文档的节点信息的方法@Testpublic void test1(){File file = new File("./src/contact.xml");Document doc = readFile(file);Iterator<Node> iterator = doc.nodeIterator();while(iterator.hasNext()){Node node = iterator.next();System.out.println(node.getName());if(node instanceof Element){Element element = (Element)node;Iterator<Node> iterator2 = element.nodeIterator();while(iterator2.hasNext()){Node node2 = iterator2.next();System.out.println(node2.getName());}}}//得到的结果为contacts.}//遍历文档中的所有节点 包括 文本节点, 标签节点@Testpublic void test2(){File file = new File("./src/contact.xml");Document doc = readFile(file);Element rootElement = doc.getRootElement();StringBuffer sb = new StringBuffer();getChild(rootElement, sb);System.out.println(sb.toString());}private void getChild(Element element, StringBuffer sb){sb.append("<" + element.getName());List<Attribute> attrs = element.attributes();Iterator<Attribute> iterator = attrs.iterator();while(iterator.hasNext()){Attribute attr = iterator.next();sb.append(" "+attr.getName()+"="+"\""+attr.getValue()+"\"");}sb.append(">");Iterator<Node> nodeIt = element.nodeIterator();while(nodeIt.hasNext()){Node node = nodeIt.next();if(node instanceof Element){getChild((Element)node, sb);}if(node instanceof Text){Text text = (Text)node;sb.append(text.getText());}}sb.append("</"+element.getName()+">");}//该方法演示得到子标签 的方法@Testpublic void test3(){File file = new File("./src/contact.xml");Document doc = readFile(file);Element rootElement = doc.getRootElement();//得到指定名字的第一个元素
// Element element = rootElement.element("contact");
// System.out.println(element.getName());
//
// System.out.println("-----------------------------");//得到指定名字的所有元素
// Iterator<Element> iterator = rootElement.elementIterator("contact");
// while(iterator.hasNext()){
// Element element2 = iterator.next();
// System.out.println(element2.getName());
// }//得到所有的子元素List<Element> elements = rootElement.elements();Iterator<Element> iterator = elements.iterator();while(iterator.hasNext()){Element element = iterator.next();System.out.println(element.getName());}}public static List<Contact> getContacts(File file){Document doc = readFile(file);Element rootElement = doc.getRootElement();List<Contact> contacts = new ArrayList<Contact>();Iterator<Element> iterator = rootElement.elementIterator("contact");while(iterator.hasNext()){Contact contact = new Contact();Element element = iterator.next();contact.setId(element.attributeValue("id"));contact.setName(element.elementText("name"));contact.setAge(element.elementText("age"));contact.setPhone(element.elementText("phone"));contact.setEmail(element.elementText("email"));contact.setQq(element.elementText("qq"));contacts.add(contact);}return contacts;}
}
其中contact.xml如下
<?xml version="1.0" encoding="utf-8"?>
<contactList><contact id="001"><name>张三</name><age>20</age><phone>134222223333</phone><email>zhangsan@qq.com</email><qq>432221111</qq></contact><contact id="002"><name>李四</name><age>20</age><phone>134222225555</phone><email>lisi@qq.com</email><qq>432222222</qq></contact>
</contactList>
这篇关于XML-dom4j实战的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!