import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Iterator;
import java.util.List;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.XMLOutputter;
import com.ailk.xmlparserdemon.intf.XmlDocument;
public class JdomXmlDemon implements XmlDocument {
static String filename;
public void createDocument(String fileName) {
System.out.println("jdom create begin...");
Document dc;
Element root;
root=new Element("employees");
dc=new Document(root);
Element employee=new Element("employee");
root.addContent(employee);
Element name=new Element("name");
name.setText("小明");
employee.addContent(name);
Element sex=new Element("sex");
sex.setText("m");
employee.addContent(sex);
Element parents=new Element("parents");
employee.addContent(parents);
Element father=new Element("father");
father.setText("小张");
parents.addContent(father);
Element mother=new Element("mother");
mother.setText("小慧");
parents.addContent(mother);
try {
XMLOutputter out=new XMLOutputter();
out.output(dc, new FileOutputStream(fileName));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("jdom create end...");
}
public void parserDocument(String fileName) {
System.out.println("jdom parser begin...");
try {
SAXBuilder build=new SAXBuilder(false);
Document dt= build.build(fileName);
Element employees=dt.getRootElement();
List employee=employees.getChildren("employee");
for(int i=0;i<employee.size();i++){
Element elements=(Element)employee.get(i);
List employinfo=elements.getChildren();
for(int j=0;j<employinfo.size();j++){
Element employinfonode=(Element)employinfo.get(j);
for(Iterator info=employinfonode.getChildren().iterator();info.hasNext();){
Element node=(Element) info.next();
System.out.println(node.getName()+":"+node.getValue());
}
if(!employinfonode.getChildren().iterator().hasNext()){
System.out.println(((Element)employinfo.get(j)).getName()+":"+((Element)employinfo.get(j)).getValue());
}
}
}
} catch (JDOMException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
System.out.println("jdom parser end...");
}
public static void main(String[] args){
JdomXmlDemon jdom=new JdomXmlDemon();
JdomXmlDemon.filename="E:\\OneNote\\jdom.xml";
jdom.createDocument(filename);
jdom.parserDocument(filename);
}
}
分享到:
相关推荐
Java解析XML汇总DOM_SAX_JDOM_DOM4j_XPath 实例
XML_XSL_DTD_SCHEMA_SAX_DOM_JDOM_WEBSERVICE简介.pdf
jdom xml 解析xml文件,可以dom4j比较
一个java操作xml的完整示例,采取jdom实现
NULL 博文链接:https://xurichusheng.iteye.com/blog/1518547
利用JDOM解析xml文件,并根据xml中为类名,生成相应的对象。
xml数据前后台解析;提供了两种方式:dom4j与jdom对xml文件、xml字符串的读与写操作;同时实现在前台用jquery解析xml,将数据展示在页面上,程序可以跑
解析xml的包,对于早java中队xml数据包的解析,速度性能比较不错的选择,xml格式是网上数据传输的标准格式
JAVA的Jdom包,解析XML不可少
jdom解析xml文件小实例jdom解析xml文件小实例jdom解析xml文件小实例
一个关于用jdom解析xml,遍历xml,建立xml的简单例子.
jdom解析xml文件的源代码、jar包以及示例xml文件
xml文件生成工具类(含jdom.jar)
NULL 博文链接:https://zhaohong.iteye.com/blog/426761
jdom 是一个处理xml文件的java类库,可以方便地对xml文件进行读或写
The JDOM build system is based on Jakarta Ant, which is a Java building tool originally developed for the Jakarta Tomcat project but now used in many other Apache projects and extended by many ...
利用Jdom生成XML与解析XML的示例
xstream、dom4j、groovy、jdom解析xml,所给的xml的例子比较全吧,基本搞懂了这些,这四种解析就差不多了
JAVA 解析XML和生成XML文档源码。比较全 1.DOM生成和解析XML文档 2.SAX生成和解析XML文档 3.DOM4J生成和解析XML文档 4.JDOM生成和解析XML
jdom解析XML文件and把字符串解析成XML.txt jdom解析XML文件and把字符串解析成XML.txtjdom解析XML文件and把字符串解析成XML.txt