jdom遍历xml,给节点Dewey编码

我想在用jdom解析xml文件的同时,为节点进行如下编码(其实就是Dewey编码),并且输出顺序要如下所示:
books:1
booka:11
id:111
0001:1111
page:112
365:1121
title:113
802.11 Wireless Networks:1131
price:114
79.00:1141
author:115
name:1151
Matthew S.Gast:11511
introduce:1152
无线网络规划和部署方面的权威作者:11521
translator:116
O’Reilly:1161
bookb:12
id:121
0002:1211
page:122
568:1221
title:123
TCP/IP Illustracted:1231
price:124
45.00:1241
author:125
name:1251
W.Richard Stevens:12511
introduce:1252
国际知名的Unix和网络专家:12521
translator:126
范建华:1261

mine.xml文件如下:
<?xml version="1.0" encoding="UTF-8"?>
<books>
<booka id="0001" page="365">
<title>802.11 Wireless Networks</title>
<price>79.00</price>
<author>
<name>Matthew S.Gast </name>
<introduce>无线网络规划和部署方面的权威作者</introduce>
</author>
<translator>O'Reilly</translator>
</booka>
<bookb id="0002" page="568">
<title>TCP/IP Illustracted</title>
<price>45.00</price>
<author>
<name>W.Richard Stevens </name>
<introduce>国际知名的Unix和网络专家</introduce>
</author>
<translator>范建华</translator>
</bookb>
</books>

下面附上Cute3.java(利用jdom解析xml,即遍历),希望能够对下面的Cute3.java修改从而能够实现我所说的编码,应该如何来修改下面的java代码呢?
import org.jdom.*;
import org.jdom.input.SAXBuilder;
import java.io.IOException;
import java.util.*;

public class Cute3 {
public static void main(String args[]){
String url = "D:\\netbeanssy\\Jdom\\src\\jdom\\mine.xml";
try {
SAXBuilder parser = new SAXBuilder();
Document document = parser.build(url);
process(document.getRootElement());
} catch (JDOMException e) {
System.out.println(url + " is not well-formed.");
} catch (IOException e) {
System.out.println("Due to an IOException,the parser could not encode "+ url);
}
}
public static void process(Element element) {
List content = element.getContent();
Iterator iterator = content.iterator();
String qualifiedName = element.getQualifiedName();
System.out.println(qualifiedName + ":" + element.getTextNormalize());
List attributes = element.getAttributes();
if (!attributes.isEmpty()) {
Iterator iterator1 = attributes.iterator();
while (iterator1.hasNext()) {
Attribute attribute = (Attribute) iterator1.next();
String name = attribute.getName();
String value = attribute.getValue();
System.out.println(name + ':' + value);
}
}
while (iterator.hasNext()) {
Object o = iterator.next();
if (o instanceof Element) {
Element child = (Element)o;
process(child);
}
}
}
}
<?xml version="1.0" encoding="UTF-8"?>
<books>
<booka id="0001" page="365">
<title>802.11 Wireless Networks: The Definitive Guide, Second Edition </title>
<year>2007</year>
<author>
<name sex="female">Matthew S.Gast</name>
<introduce>无线网络规划和部署方面的权威作者</introduce>
</author>
<translator>O'Reilly</translator>
</booka>
<bookb id="0002" page="568">
<title>TCP/IP Illustracted Volume 1:The Protocols </title>
<year>2004</year>
<author>
<name sex="male">W.Richard Stevens </name>
<introduce>国际知名的Unix和网络专家</introduce>
</author>
<translator>范建华</translator>
</bookb>
</books>

可以实现上述效果的修改后的详细代码信息如下:
Java code:
import org.jdom.*;
import org.jdom.input.SAXBuilder;
import java.io.IOException;
import java.util.*;

public class Cute3 {
public static void main(String args[]){
String url = "G:\\mine.xml";
try {
SAXBuilder parser = new SAXBuilder();
Document document = parser.build(url);
process(document.getRootElement(), "", 1);
} catch (JDOMException e) {
System.out.println(url + " is not well-formed.");
} catch (IOException e) {
System.out.println("Due to an IOException,the parser could not encode "+ url);
}
}
public static void process(Element element, String s, int n) {
List content = element.getContent();
Iterator iterator = content.iterator();
String qualifiedName = element.getQualifiedName();
System.out.println(qualifiedName + " : " + s + (new Integer(n)).toString());
int m = 1;
List attributes = element.getAttributes();
if (!attributes.isEmpty()) {
Iterator iterator1 = attributes.iterator();
while (iterator1.hasNext()) {
Attribute attribute = (Attribute) iterator1.next();
String name = attribute.getName();
String value = attribute.getValue();
System.out.println(name + " : " + s + (new Integer(n)).toString() + "." + (new Integer(m)).toString());
if(!value.equals("")){
System.out.println(value + " : " + s + (new Integer(n)).toString() + "." + (new Integer(m)).toString() + ".1");
}
m++;
}
}
if(!element.getTextNormalize().equals("")){
System.out.println(element.getTextNormalize() + " : " + s + (new Integer(n)).toString() + "." + m);
}
while (iterator.hasNext()) {
Object o = iterator.next();
if (o instanceof Element) {
Element child = (Element)o;
process(child, s + (new Integer(n)).toString() + ".", m);
m++;
}
}
n++;
}
}

此文由Web开发之答疑解惑源www.znjcx.com整理,如需转载,请注明原文出处:http://www.znjcx.com/html/y2012/3422_jdom-traversing-xml-dewey-to-node-encoding.html,谢谢!

本文出自 “Web开发之答疑解惑源” 博客,转载请与作者联系!

你可能感兴趣的:(xml,jdom,遍历,节点,dewey编码)