package com.book.jdom;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;
//生成xml文件
public class CreateJdom {
public static void main(String[] args) {
//定义元素
Element people,student;
people = new Element("people");
student = new Element("student");
//设置属性
student.setAttribute("name", "张三");
student.setAttribute("salary","8000");
//设置文本
student.setText("呵呵");
//将其添加到根目录下
people.addContent(student);
//新建一个文档。
Document doc = new Document(people);
//读取格式,赋值给当前的Format
Format format = Format.getCompactFormat();
//对当前格式进行初始化
format.setEncoding("UTF-8");
//设置xml文件缩进4个空格
format.setIndent(" ");
//建一个xml输出工厂,将格式给工厂
XMLOutputter xmlout = new XMLOutputter(format);
try {
//将其写好的文本给工厂,并且建一个文件输出流,将数据输出
xmlout.output(doc, new FileOutputStream("people.xml"));
System.out.println("成功!");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
/*运行结果:
<?xml version="1.0" encoding="UTF-8"?>
<people>
<student name="张三" salary="8000" />
</people>
* */
package com.book.jdom;
import java.io.IOException;
import java.util.List;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
//读取people.xml文档
public class Readxml {
public static void main(String[] args) {
//新建构造器解析xml
SAXBuilder sax = new SAXBuilder();
//建一个文档去接受数据
Document doc;
try {
//获取people.xml文档
doc = sax.build("people.xml");
//获得根节点
Element people = doc.getRootElement();
//获得根节点下的节点数据
List<Element> list = people.getChildren();
for(int i = 0;i<list.size();i++){
Element e = list.get(i);
//获得属性值
System.out.println("name:"+e.getAttributeValue("name")+" salary:"+e.getAttributeValue("salary"));
//获得文本值
System.out.println(e.getText());
}
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
/*
* 运行结果:
* name:张三 salary:8000
呵呵
* */
用jdom获取多个相同标签名的不同属性值的方法
<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Key Name="China">
<Value Name="TextKey">China</Value>
<Value Name="Enabled">true</Value>
<Value Name="PhotoIDWidth">38PhotoIDWidth</Value>
<Value Name="PhotoIDHeight">38</Value>
<Key Name="Adult">
<Value Name="CrownPercent">0.10</Value>
<Value Name="HeadPercent">0.60AdultHeadPercent</Value>
</Key>
<Key Name="Child">
<Value Name="CrownPercent">0.10</Value>
<Value Name="HeadPercent">0.60ChildHeadPercent</Value>
</Key>
</Key>
<Key Name="Australia">
<Value Name="TextKey">Australia</Value>
<Value Name="Enabled">true</Value>
<Value Name="PhotoIDWidth">35PhotoIDWidth</Value>
<Value Name="PhotoIDHeight">45</Value>
<Key Name="Adult">
<Value Name="CrownPercent">0.061</Value>
<Value Name="HeadPercent">0.756"Adult"HeadPercent</Value>
</Key>
<Key Name="Child">
<Value Name="CrownPercent">0.072</Value>
<Value Name="HeadPercent">0.711ChildHeadPercent</Value>
</Key>
</Key>
<Key Name="Austria">
<Value Name="TextKey">Austria</Value>
<Value Name="Enabled">true</Value>
<Value Name="PhotoIDWidth">35PhotoIDWidth</Value>
<Value Name="PhotoIDHeight">45</Value>
<Key Name="Adult">
<Value Name="CrownPercent">0.064</Value>
<Value Name="HeadPercent">0.744AdultHeadPercent</Value>
</Key>
<Key Name="Child">
<Value Name="CrownPercent">0.078</Value>
<Value Name="HeadPercent">0.689ChildHeadPercent</Value>
</Key>
</Key>
</Configuration>
package input;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
public class ReadXML {
/**
* @param args
*/
public static void main(String[] args) throws JDOMException, IOException {
SAXBuilder sb = new SAXBuilder();
//构造文档对象
Document doc = sb.build(Test.class.getClassLoader().getResourceAsStream("nation.xml"));
//获取根元素
Element root = doc.getRootElement();
//定位到<Configuration> -> <Key>
List<Element> list = root.getChildren("Key");
List<Element> children = new ArrayList<Element>();
List<Element> childrens = new ArrayList<Element>();
for (int i = 0; i < list.size(); i++) {
Element element = (Element) list.get(i);
System.out.print(element.getAttributeValue("Name"));
//定位到<Configuration> -> <Key> -> <Value>
children = element.getChildren("Value");
for(int j=0; j<children.size(); j++){
Element elementChildren = (Element) children.get(j);
//定位到<Configuration> -> <Key> -> <Value Name="PhotoIDWidth">
if(elementChildren.getAttributeValue("Name").equals("PhotoIDWidth")){
//获取<Configuration> -> <Key> -> <Value Name="PhotoIDWidth"> 属性值
System.out.print("<--------->"+elementChildren.getAttributeValue("Name"));
//获取<Configuration> -> <Key> -> <Value Name="PhotoIDWidth"> 标签里内容
System.out.print(","+elementChildren.getText());
}
}
children.clear();
//定位到<Configuration> -> <Key> -> <Key>
children = element.getChildren("Key");
for(int j=0; j<children.size(); j++){
Element elementChildren = (Element)children.get(j);
//定位到<Configuration> -> <Key> -> <Key Name="Child">
if(elementChildren.getAttributeValue("Name").equals("Child")){
//定位到<Configuration> -> <Key> -> <Key Name="Child"> -> <Value>
childrens = elementChildren.getChildren("Value");
for(int k=0; k<childrens.size(); k++){
Element elementChildrens = (Element)childrens.get(k);
//定位到<Configuration> -> <Key> -> <Key Name="Child"> -> <Value Name="HeadPercent">
if(elementChildrens.getAttributeValue("Name").equals("HeadPercent")){
System.out.println("<--------->"+elementChildrens.getText());
}
}
}
}
}
}
}
打印结果:
China<--------->PhotoIDWidth,38PhotoIDWidth<--------->0.60ChildHeadPercent
Australia<--------->PhotoIDWidth,35PhotoIDWidth<--------->0.711ChildHeadPercent
Austria<--------->PhotoIDWidth,35PhotoIDWidth<--------->0.689ChildHeadPercent
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有