java xml jdom创建 修改 解析
2017-04-03 10:35
417 查看
注:需要jdom的jar包
package dom.parse;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
public class JDomTest {
public static void main(String[] args) throws FileNotFoundException, IOException, JDOMException {
//createJDomXml();
String path="src/dom/parse/jdom.xml";
//modifyJDomXml(path);
parseXml(path);
}
private static void parseXml(String path) throws JDOMException, IOException {
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(path);
Element root = doc.getRootElement();
@SuppressWarnings("unchecked")
List<Element> elementlist = root.getChildren();
List<Employee1> employee1List = new ArrayList<Employee1>();
for (int i = 0; i < elementlist.size(); i++) {
Employee1 employee1 = new Employee1();
Element e = elementlist.get(i);
@SuppressWarnings("unchecked")
List<Attribute> attributeList = e.getAttributes();
String employeeId=null;
String employeeName=null;
for (int j = 0; j < attributeList.size(); j++) {
if(j==0){
employeeId=attributeList.get(j).getValue();
}
if(j==1){
employeeName=attributeList.get(j).getValue();
}
}
employee1.setEmployeeId(employeeId);
employee1.setEmployeeName(employeeName);
employee1.setContent(e.getValue());
List<Jiangli> jingliList = new ArrayList<Jiangli>();
Jiangli jiangli= new Jiangli();
if(null != e.getChildren())
{
List<Element> childrenList = e.getChildren();
for (int j = 0; j < childrenList.size(); j++) {
Element child = childrenList.get(j);
//System.out.println(child);
List<Attribute> attributeChildrenList = child.getAttributes();
Attribute childrenAttribute=null;
String jiangliName=null;
String year=null;
for (int k = 0; k < attributeChildrenList.size(); k++) {
if(k==0)
{
jiangliName=attributeChildrenList.get(k).getValue();
}
if(k==1)
{
year=attributeChildrenList.get(k).getValue();
}
}
jiangli.setJiangliName(jiangliName);
jiangli.setYear(year);
jiangli.setContent(child.getValue());
//System.out.println(jiangli);
jingliList.add(jiangli);
}
}
employee1.setJianglis(jingliList);
//employee1List.add(employee1);
System.out.println(employee1);
//System.out.println(employee1List);
}
}
private static void modifyJDomXml(String path) throws JDOMException, IOException {
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(path);
Element root = doc.getRootElement();
System.out.println(root);
System.out.println(root.getName());
/* Element employee1= root.getChild("employee");
List list=employee1.getAttributes();
for (int i = 0; i < list.size(); i++) {
Attribute attr = (Attribute) list.get(i);
System.out.println(attr.getName()+"--->"+attr.getValue());
}*/
List<Element> list = root.getChildren();
for (int i = 0; i < list.size(); i++) {
Element e = list.get(i);
System.out.println(e.getName()+"---->"+e.getValue());
if (i==2) {
Element zi=list.get(i).getChild("jiangli");
System.out.println(" "+zi.getName()+"---->"+zi.getValue());
e.removeChild(zi.getName());
}
}
XMLOutputter output = new XMLOutputter(Format.getPrettyFormat());
output.output(doc,new FileOutputStream("src/dom/parse/jdom2.xml") );
}
private static void createJDomXml() throws FileNotFoundException, IOException {
Document document = new Document();
Element root = new Element("employees");
document.addContent(root);
Element e1 = new Element("employee");
e1.setAttribute("employeeId","1");
e1.setAttribute("employeeName","zhangsan");
e1.setText("xianjiaoda");
root.addContent(e1);
Element e2 = new Element("employee");
Attribute attr = new Attribute("employeeId","2");
e2.setAttribute(attr);
root.addContent(e2);
Element e3 = new Element("employee");
e3.setAttribute("employeeId","3").setAttribute("name","wangwu");
e3.addContent(new Element("jiangli").setAttribute("sanhaoxuesheng","2012").setAttribute("sanhaobiaobing","2013").setText("xianjiaoda"));
root.addContent(e3);
Format format = Format.getPrettyFormat();
format.setIndent(" ");
//format.setEncoding("gbk");
XMLOutputter output = new XMLOutputter(format);
output.output(document,new FileOutputStream("src/dom/parse/jdom.xml"));
}
}
注:需要jdom的jar包
package dom.parse;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
public class JDomTest {
public static void main(String[] args) throws FileNotFoundException, IOException, JDOMException {
//createJDomXml();
String path="src/dom/parse/jdom.xml";
//modifyJDomXml(path);
parseXml(path);
}
private static void parseXml(String path) throws JDOMException, IOException {
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(path);
Element root = doc.getRootElement();
@SuppressWarnings("unchecked")
List<Element> elementlist = root.getChildren();
List<Employee1> employee1List = new ArrayList<Employee1>();
for (int i = 0; i < elementlist.size(); i++) {
Employee1 employee1 = new Employee1();
Element e = elementlist.get(i);
@SuppressWarnings("unchecked")
List<Attribute> attributeList = e.getAttributes();
String employeeId=null;
String employeeName=null;
for (int j = 0; j < attributeList.size(); j++) {
if(j==0){
employeeId=attributeList.get(j).getValue();
}
if(j==1){
employeeName=attributeList.get(j).getValue();
}
}
employee1.setEmployeeId(employeeId);
employee1.setEmployeeName(employeeName);
employee1.setContent(e.getValue());
List<Jiangli> jingliList = new ArrayList<Jiangli>();
Jiangli jiangli= new Jiangli();
if(null != e.getChildren())
{
List<Element> childrenList = e.getChildren();
for (int j = 0; j < childrenList.size(); j++) {
Element child = childrenList.get(j);
//System.out.println(child);
List<Attribute> attributeChildrenList = child.getAttributes();
Attribute childrenAttribute=null;
String jiangliName=null;
String year=null;
for (int k = 0; k < attributeChildrenList.size(); k++) {
if(k==0)
{
jiangliName=attributeChildrenList.get(k).getValue();
}
if(k==1)
{
year=attributeChildrenList.get(k).getValue();
}
}
jiangli.setJiangliName(jiangliName);
jiangli.setYear(year);
jiangli.setContent(child.getValue());
//System.out.println(jiangli);
jingliList.add(jiangli);
}
}
employee1.setJianglis(jingliList);
//employee1List.add(employee1);
System.out.println(employee1);
//System.out.println(employee1List);
}
}
private static void modifyJDomXml(String path) throws JDOMException, IOException {
SAXBuilder builder = new SAXBuilder();
Document doc = builder.build(path);
Element root = doc.getRootElement();
System.out.println(root);
System.out.println(root.getName());
/* Element employee1= root.getChild("employee");
List list=employee1.getAttributes();
for (int i = 0; i < list.size(); i++) {
Attribute attr = (Attribute) list.get(i);
System.out.println(attr.getName()+"--->"+attr.getValue());
}*/
List<Element> list = root.getChildren();
for (int i = 0; i < list.size(); i++) {
Element e = list.get(i);
System.out.println(e.getName()+"---->"+e.getValue());
if (i==2) {
Element zi=list.get(i).getChild("jiangli");
System.out.println(" "+zi.getName()+"---->"+zi.getValue());
e.removeChild(zi.getName());
}
}
XMLOutputter output = new XMLOutputter(Format.getPrettyFormat());
output.output(doc,new FileOutputStream("src/dom/parse/jdom2.xml") );
}
private static void createJDomXml() throws FileNotFoundException, IOException {
Document document = new Document();
Element root = new Element("employees");
document.addContent(root);
Element e1 = new Element("employee");
e1.setAttribute("employeeId","1");
e1.setAttribute("employeeName","zhangsan");
e1.setText("xianjiaoda");
root.addContent(e1);
Element e2 = new Element("employee");
Attribute attr = new Attribute("employeeId","2");
e2.setAttribute(attr);
root.addContent(e2);
Element e3 = new Element("employee");
e3.setAttribute("employeeId","3").setAttribute("name","wangwu");
e3.addContent(new Element("jiangli").setAttribute("sanhaoxuesheng","2012").setAttribute("sanhaobiaobing","2013").setText("xianjiaoda"));
root.addContent(e3);
Format format = Format.getPrettyFormat();
format.setIndent(" ");
//format.setEncoding("gbk");
XMLOutputter output = new XMLOutputter(format);
output.output(document,new FileOutputStream("src/dom/parse/jdom.xml"));
}
}
相关文章推荐
- 【java开发系列】—— JDOM创建、修改、删除、读取XML文件
- 【Java开发】--jdom解析xml-修改xml
- java中利用dom4j对XML文档的创建、解析、查找、修改、保存等操作。
- 【Java开发】--jdom解析xml-创建xml
- java jdom解析和创建xml
- java中利用dom4j对XML文档的创建、解析、查找、修改、保存等操作。
- java xml dom4j 创建 修改 解析
- 【java开发系列】— JDOM创建、修改、删除、读取XML文件
- java JDOM解析XML字符串(非XML文档)
- xml解析(jdom java)
- Java程序员从笨鸟到菜鸟之(二十七)XML之Jdom和DOM4J解析 .
- java 解析/操作 xml 几种常用方式 xml的增加/删除/修改
- Java学习之路——利用JDOM对xml文档进行解析等操作实例
- 使用JDOM创建和解析XML文件
- java解析xml(JDOM)配置环境
- Jdom操作、创建和解析XML
- Java程序员从笨鸟到菜鸟之(二十七)XML之Jdom和DOM4J解析 .
- java中用jdom 修改xml
- JAVA jdom 解析 xml
- [JAVA100例]070、使用JDOM解析XML