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,謝謝!
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章