欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Jdom生成XML和获取XML数据的方法

程序员文章站 2022-05-28 12:17:16
...
[b]今天项目中要用到XML,生成两个XML文件并向其中写入内容,并能读取XML的内容。用Jdom([color=red]Jdom是用Java语言读、写、操作XML的新API函数。Jdom是基于树操作的纯Java API,是一套用于解析、创建和实现xml的解决方案。[/color])写的一个类,感觉应该可以通用,发到博客,希望能和大家多交流交流。[/b]

[b]思路:[/b]用JDOM读取XML文件,需先用org.jdom.input.SAXBuilder对象的build()方法创建Document对象,然后用Document类(org.jdom.Document)、Element类(org.jdom.Element)等方法读取所需的内容。


Jdom模型:

每个元素都有四个关键段:名称、元素属性、元素范围名字空间、元素内容

用JDOM处理现有XML文档的大致过程如下:

(1)用简单无变元构造函数构造一个org.jdom.input.SAXBuilder对象。SAXBuilder用sax解析器从文件中构造文档。SAXBuilder侦听sax事件并从内存中建立一个相应的文档。

(2)用SAXBuiler的build()方法从Reader,InputStream,URL,File或包含系统ID的字符串建立Document对象。

(3)如果读取文档遇到问题,则抛出IOException,如果建立文档遇到问题,则抛出JDOMException。

(4)否则用Document类,Element类和其他JDOM类的方法在文档中建立导航。


导航JDOM树

每个Element对象包含一列子元素:Comment,ProcessingInstruction,Text和其他Element对象。此外,属性和其他名字空间还有单独的列表。

分析文档和建立Document对象之后,可能要通过搜索来选择其中程序感兴趣的部分。在JDOM中,大多数导航通过Element类的方法进行。

每个Element的完整子元素(包括了子元素的所有内容,包括说明、处理指令、文本节点和元素,要进行深度搜索,就要对当前元素的子元素应用getContent()方法,通常要采用递归)在getContent()方法返回java.util.List中提供。getChildren()方法返回的java.util.List中只有每个Element的子元素。

JDOM的处理主要是用SAX实现的, JDOM中几乎没有接口,全部是实实在在的类,没有类工厂类的。其最重要的一个包org.jdom中主要有以下类:

Document(文档节点):每个Document对象包括下列三个属性:根Element;表示文档类型声明的DocType对象;包含根元素和任何处理指令与说明的List,按文档顺序出现。

Attribute(属性节点):

Public Element setAttributes (List attributes)Throws IllegalAddException; Public List getAttributes() ;

setAttribute();getAttribute();getAttributeValue();

attribute.getName() ;attribute.getValue()

CDATA (CDATA段节点):
Comment(说明节点) ,即XML文件的说明:<!-- wire configuration -->

DocType (文档类型节点)
Element(元素节点) 元素名设置和获取:

Public Element setName(String name) throws IllegalNameException;Public String getName();

Public String get(int i) //i>=0
Content(内容节点)

Public Element setContent(List list) throws IllegalAddException;
public List getContent(); addContent(); removeContent();

EntityRef(实例节点)

Namespace(名字空间节点)

ProcessingInstruction(处理指令节点)

Text(文本节点):getText();setText(String s);



package cn.cubetech.xian210;

import java.io.FileOutputStream;
import java.util.List;


import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

public class CreateAndReadXML {

public void createFormXML(){
Element root = new Element("root");
Document doc = new Document(root);

Element element1 = new Element("data");
Element element2 = new Element("return");
Element element3 = new Element("OaOutSendDocDoc");

element1.addContent(new Element("docID").setText("21361"));
element1.addContent(element3);

element2.addContent(new Element("code").setText("11111111"));

element3.setAttribute("criteria", "=");
element3.setAttribute("rowNum", "1");
element3.addContent(new Element("docID").setText("21361"));
element3.addContent(new Element("fileNo").setText("AA"));
element3.addContent(new Element("title"));
element3.addContent(new Element("draftMan"));
element3.addContent(new Element("processInstID"));
element3.addContent(new Element("urgentLev"));
element3.addContent(new Element("status"));
element3.addContent(new Element("remark"));
element3.addContent(new Element("refb"));
element3.addContent(new Element("dept"));
element3.addContent(new Element("sourceDocID"));
element3.addContent(new Element("receiveDept"));
element3.addContent(new Element("sendsrLev"));
element3.addContent(new Element("wfType"));
element3.addContent(new Element("orderID"));
element3.addContent(new Element("refa"));
element3.addContent(new Element("sourceType"));
element3.addContent(new Element("sendDate"));

root.addContent(element1);
root.addContent(element2);

XMLOutputter XMLOut = new XMLOutputter();
Format format = Format.getPrettyFormat();format.setEncoding("UTF-8");
format.setIndent(" ");
XMLOut.setFormat(format);
try {
XMLOut.output(doc, new FileOutputStream("C:/testForm.xml"));
System.out.println("----------生成testForm.xml成功----------");
} catch (Exception e) {
e.printStackTrace();
}
}

public void createAttachmentXML(){
Element root = new Element("root");
Document doc = new Document(root);

Element element1 = new Element("data");
Element element2 = new Element("FbFileVSubFileInfo");
Element element21 = new Element("fileName").setText("cf.txt");
Element element22 = new Element("absolutePath").setText("upload\\OA\\201104\\OUTSENDDOC\\10261.txt");
Element element3 = new Element("FileEntity");
Element element31 =new Element("content").setText("ZGVsZXRlIGZyb20gc21fcm9sZV9jb3JwX2FsbG9jIHdoZXJlIHBrX2NvcnAgbm90IGluKHNlbGVjdCBwa19jb3JwIGZyb20gYmRfY29ycCk7DQpjb21taXQ7");

element2.addContent(element21);
element2.addContent(element22);

element3.addContent(element31);

element1.addContent(element2);
element1.addContent(new Element("EOSFile_RootPath").setText("I:/server_oa/eosdomain/"));
element1.addContent(new Element("filepath").setText("I:/server_oa/eosdomain/upload\\OA\\201104\\OUTSENDDOC\\10261.txt"));
element1.addContent(element3);
element1.addContent(new Element("FILE_NAME").setText("cf.txt"));
element1.addContent(new Element("File_Format").setText("txt"));
element1.addContent(new Element("fileNameUUID").setText("935bca33665e11e08de9c3507b66b1f1"));

root.addContent(element1);

XMLOutputter XMLOut = new XMLOutputter();
Format format = Format.getPrettyFormat();format.setEncoding("UTF-8");
format.setIndent(" ");
XMLOut.setFormat(format);
try {
XMLOut.output(doc, new FileOutputStream("C:/testAttachment.xml"));
System.out.println("----------生成testAttachment.xml成功----------");
} catch (Exception e) {
e.printStackTrace();
}

}

public void readFormXML() throws Exception{
SAXBuilder sb=new SAXBuilder();
Document doc=sb.build("C:/testForm.xml");
Element root=doc.getRootElement();
List list1=root.getChildren("data");
String docID = "";
String criteria = "";
String rowNum = "";
String fileNo = "";
String title = "";
String draftMan = "";
String processInstID = "";
String urgentLev = "";
String status = "";
String remark = "";
String refb = "";
String dept = "";
String sourceDocID = "";
String receiveDept = "";
String sendsrLev = "";
String wfType = "";
String orderID = "";
String refa = "";
String sourceType = "";
String sendDate = "";
for(int i = 0; i < list1.size(); i++){
Element element1 = (Element)list1.get(i);
docID = element1.getChildText("docID");
System.out.println("----------docID="+docID);
Element element2 = element1.getChild("OaOutSendDocDoc");
criteria = element2.getAttributeValue("criteria");
rowNum = element2.getAttributeValue("rowNum");
title = element2.getChildText("title");
draftMan = element2.getChildText("draftMan");
processInstID = element2.getChildText("processInstID");
urgentLev = element2.getChildText("urgentLev");
status = element2.getChildText("status");
remark = element2.getChildText("remark");
refb = element2.getChildText("refb");
dept = element2.getChildText("dept");
sourceDocID = element2.getChildText("sourceDocID");
receiveDept = element2.getChildText("receiveDept");
sendsrLev = element2.getChildText("sendsrLev");
wfType = element2.getChildText("wfType");
orderID = element2.getChildText("orderID");
refa = element2.getChildText("refa");
sourceType = element2.getChildText("sourceType");
sendDate = element2.getChildText("sendDate");
}

List list2 = root.getChildren("return");
String code = "";
for(int j=0; j < list2.size(); j++){
Element element = (Element)list2.get(j);
code = element.getChildText("code");
}
}

public void readAttachmentXML() throws Exception{
SAXBuilder sb=new SAXBuilder();
Document doc=sb.build("C:/testAttachment.xml");
Element root=doc.getRootElement();
List list=root.getChildren("data");
String fileName = "";
String absolutePath = "";
String EOSFile_RootPath = "";
String filepath = "";
String content = "";
String FILE_NAME = "";
String File_Format = "";
String fileNameUUID = "";
for(int i=0; i<list.size(); i++){
Element element1 = (Element)list.get(i);
fileName = element1.getChild("FbFileVSubFileInfo").getChildText("fileName");
absolutePath = element1.getChild("FbFileVSubFileInfo").getChildText("absolutePath");
EOSFile_RootPath = element1.getChildText("EOSFile_RootPath");
filepath = element1.getChildText("filepath");
content = element1.getChild("FileEntity").getChildText("content");
FILE_NAME = element1.getChildText("FILE_NAME");
File_Format = element1.getChildText("File_Format");
fileNameUUID = element1.getChildText("fileNameUUID");
System.out.println("--------fileName="+fileName);
System.out.println("--------absolutePath="+absolutePath);
System.out.println("--------EOSFile_RootPath="+EOSFile_RootPath);
System.out.println("--------filepath="+filepath);
System.out.println("--------content="+content);
System.out.println("--------FILE_NAME="+FILE_NAME);
System.out.println("--------File_Format="+File_Format);
System.out.println("--------fileNameUUID="+fileNameUUID);
}

}

public static void main(String[] args){
CreateAndReadXML test = new CreateAndReadXML();
//test.createFormXML();
//test.createAttachmentXML();
try {
// test.readFormXML();
test.readAttachmentXML();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}



test.createFormXML()方法执行后生成的XML文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<root>
<data>
<docID>21361</docID>
<OaOutSendDocDoc criteria="=" rowNum="1">
<docID>21361</docID>
<fileNo>AA</fileNo>
<title />
<draftMan />
<processInstID />
<urgentLev />
<status />
<remark />
<refb />
<dept />
<sourceDocID />
<receiveDept />
<sendsrLev />
<wfType />
<orderID />
<refa />
<sourceType />
<sendDate />
</OaOutSendDocDoc>
</data>
<return>
<code>11111111</code>
</return>
</root>


test.createAttachmentXML()方法执行后生成的XML文件如下:

<?xml version="1.0" encoding="UTF-8"?>
<root>
<data>
<FbFileVSubFileInfo>
<fileName>cf.txt</fileName>
<absolutePath>upload\OA\201104\OUTSENDDOC\10261.txt</absolutePath>
</FbFileVSubFileInfo>
<EOSFile_RootPath>I:/server_oa/eosdomain/</EOSFile_RootPath>
<filepath>I:/server_oa/eosdomain/upload\OA\201104\OUTSENDDOC\10261.txt</filepath>
<FileEntity>
<content>ZGVsZXRlIGZyb20gc21fcm9sZV9jb3JwX2FsbG9jIHdoZXJlIHBrX2NvcnAgbm90IGluKHNlbGVjdCBwa19jb3JwIGZyb20gYmRfY29ycCk7DQpjb21taXQ7</content>
</FileEntity>
<FILE_NAME>cf.txt</FILE_NAME>
<File_Format>txt</File_Format>
<fileNameUUID>935bca33665e11e08de9c3507b66b1f1</fileNameUUID>
</data>
</root>