您的位置:首页 > 其它

JDOM解析XML文件

2007-12-02 21:06 363 查看
java操作xml用的最多的应该是jdom开源包了,它是document模式的(虽然它用到了SAX模式),主要的API如下:
1:SAXBuilder.build(FileInputStream("*.xml");获取xml文件,返回Document实例(读xml文件)
2:Element.getChildren();获取该节点的所有字节点,返回List
3:Element.getChild("child节点名");获取字节点实例
4:Element.getAttribute("属性名");获取该节点属性的value值(平面式节点)
5:Element.getText();获取该节点的节点文本
6:Document(new Element("根节点名"));新建xml文件文档
7:Document.getRootNote();获取根节点
8:Element.addContent(Element);添加子节点
9:Element.setAttribute("属性名","属性值");添加节点属性
10:Element.setText("文本值");添加该节点的文本值
11:xmloutPutter(Format.getPrettyFormat())
12:xmlOutPutter.output(Document,FileOutPutStream);这两句用来输出xml文件,其中Document为填好内容的xml文档对象,FileoutPutStream为文本输出流)

用以上的API基本可以完成所有读写xml文件的功能了,jdom封装的很不错,类很好理解,上手也很快。
唯一的不足是它不是严格校验的xml解析器,即你在getAttribute某个属性的时候,jdom并不会根据该xml文件的DTD去校验该属性是否合法,也就是说你的xml文件中完全可以不带DTD=""的索引,因为jdom不会去加载它。实际上我们的项目中也没有谁会去把我们定义的xml文件格式所表示的数据结构写成DTD定义文件,虽然从理论上它应该是和xml文件一起出现的:)

例子:

在Java中用JDOM才操作xml文件很方便,需要的代码量也比其它XML解析器要少的多。下面用一个简单的例子来说明JDOM读写xml的最基本的步骤。假设已经有如下的xml文件student.xml:

<Students>
<Student gender ="male">
<name>Tom</name>
<age>14</age>
<phone> 12345678</phone>
</Student>
</Students>

下面读取该文件中的内容并打印输出:

SAXBuilder builder = new SAXBuilder(false);
Document document = null;
try {
document = builder.build("student.xml");
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

Element root = document.getRootElement();
List students = root.getChildren();
for( int i=0; i<students.size(); i++)
{
Element student = (Element)students.get(i);
System.out.println("Attribute Gender :" + student.getAttributeValue("gender"));
List children = student.getChildren();
for( int j=0; j<children.size(); j++)
{
Element child = (Element)children.get(j);
System.out.println("Element name: "+ child.getName() );
System.out.println("Element value: " + child.getText());
}
}
运行输出的结果如下:


Attribute Gender :male


Element name: name


Element value: Tom


Element name: age


Element value: 14


Element name: phone


Element value: 12345678
下面插入一个student记录,然后保存到student.xml文件中:

//inset a record of student
Element student = new Element("Student");
student.setAttribute("gender", "female");
student.addContent(new Element("name").setText("Mary"));
student.addContent(new Element("age").setText("18"));
student.addContent(new Element("phone").setText("42483433"));

document.getRootElement().addContent(student);

try{
XMLOutputter outputter = new XMLOutputter();
Format fmt = Format.getPrettyFormat();
//缩进的长度
fmt.setIndent(" ");
outputter.setFormat(fmt);
outputter.output(root.getDocument(), new BufferedWriter(new FileWriter("student.xml")));
}catch(IOException ioe)
{
ioe.printStackTrace();
}
现在的student.xml内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<Students>
<Student gender="male">
<name>Tom</name>
<age>14</age>
<phone>12345678</phone>
</Student>
<Student gender="female">
<name>Mary</name>
<age>18</age>
<phone>42483433</phone>
</Student>
</Students>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: