导入工具包
1、新建一个文件夹lib
2、导入工具包
这里有三个包,只需要导入要用到的就行,复制粘贴到lib文件夹里
粘贴后效果是这样的
3、配置工具包
选择Project Structure
点击Libraries,点击+号选择Java
选择项目所在路径,点击OK
继续OK
选择Cancle
选中后再点删除
点击+号,选择需要的工具包,点击OK
Apply一下
回到Modules,点击+号,选择Library
接下来会发现左边的工具包已经可以打开,并且右边的导包没有再报错了,说明导入成功。
1、本地文件解析
package com.java.demo1;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
public class Demo1 {
public static void main(String[] args) throws IOException, DocumentException {
//1、获取输入流
FileInputStream fis = new FileInputStream("src/com/java/demo1/Demo1.xml");
//2、创建XML读取对象
SAXReader rs = new SAXReader();
//3、读取并得到文档对象
Document doc = rs.read(fis);
//4、通过文档获取根元素
Element root = doc.getRootElement();
//5、开始解析元素
String rootName = root.getName();
System.out.println(rootName);
// Element book = root.element("book");
// Element name = book.element("name");
// System.out.println(name.getText());
List<Element> es = root.elements();
for(int i=0; i<es.size(); i++){
Element book = es.get(i);
System.out.println(book.attributeValue("id"));
System.out.println(book.elementText("name"));
System.out.println(book.elementText("info"));
System.out.println("-----------");
}
fis.close();
}
}
2、网络url地址XML解析
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.List;
public class Demo2 {
public static void main(String[] args) throws IOException, DocumentException {
String phone ="19982794902";
//1、获取到XML资源的输入流
URL url = new URL("http://apis.juhe.cn/mobile/get?%20phone=" + phone + "&dtype=xml&key=9f3923e8f87f1ea50ed4ec8c39cc9253");
URLConnection conn = url.openConnection();
InputStream is = conn.getInputStream();
//2、创建一个XML读取对象
SAXReader sr = new SAXReader();
//3、通过读取对象 读取XML数据,并返回文档对象
Document doc = sr.read(is);
//4、获取根节点
Element root = doc.getRootElement();
//5、解析内容
String code = root.elementText("resultcode");
if("200".equals(code)){
Element result = root.element("result");
String province = result.elementText("province");
String city = result.elementText("city");
if(province.equals(city)){
System.out.println("手机号码归属地为:" + city);
}else{
System.out.println("手机号码归属地为:" + province + " " + city);
}
}else{
System.out.println("请输入正确的手机号码");
}
}
}
3、本地XML文件生成
package com.java.demo1;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.XMLWriter;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
public class Demo5 {
public static void main(String[] args) throws IOException {
//1、通过文档帮助器,创建一个文档对象
Document doc = DocumentHelper.createDocument();
//2、给文档添加第一个节点(根节点)
Element books = doc.addElement("books");
//3、通过根节点,丰富子节点
for(int i=0; i<2; i++){
Element book = books.addElement("book");
Element name = book.addElement("name");
name.setText(i+"种苹果");
Element info = book.addElement("info");
info.setText(i+"好吃");
book.addAttribute("id", 100+i+"");
}
//4、创建文件输出流
FileOutputStream fos = new FileOutputStream("apple.xml");
//5、将输出流转换为XML输出流
XMLWriter xw = new XMLWriter(fos);
//6、写出文档
xw.write(doc);
//7、释放资源
xw.close();
System.out.println("写入成功");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<books>
<book id="100">
<name>0种苹果</name>
<info>0好吃</info>
</book>
<book id="101">
<name>1种苹果</name>
<info>1好吃</info>
</book>
</books>
4、XStream对象生成xml
package com.java.demo1;
import com.thoughtworks.xstream.XStream;
import java.util.Objects;
public class Demo6 {
public static void main(String[] args) {
Person p = new Person();
p.setName("张三");
p.setAge(18);
//XStream使用
//1、创建XStream对象
XStream x = new XStream();
//2、修改某个类型生成的根节点(可选的, 默认为包名.类名)
x.alias("person",Person.class);
//3、传入对象,开始生成
String xml = x.toXML(p);
System.out.println(xml);
}
static class Person {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String nae) {
this.name = nae;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Person() {
}
@Override
public String toString() {
return "Person{" +
"nae='" + name + '\'' +
", age=" + age +
'}';
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Person person = (Person) o;
return age == person.age && Objects.equals(name, person.name);
}
@Override
public int hashCode() {
return Objects.hash(name, age);
}
}
}
执行结果