要解决的问题是:当jdom遍历xml时输出节点顺序错误,下面是Cute2.java文件:
import org.jdom.*;
import org.jdom.input.SAXBuilder;
import java.io.IOException;
import java.util.*;
public class Cute2{
public static void process(Element element){
List content=element.getContent();
Iterator iterator=content.iterator();
while(iterator.hasNext()){
Object o=iterator.next();
if(o instanceof Element){
Element child=(Element)o;
process(child);
}
}
String qualifiedName=element.getQualifiedName();
System.out.println(qualifiedName+":"+element.getText());
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();
}
}
}
public static void main(String[] args){
String url="D:\\netbeanssy\\Jdom\\src\\jdom\\example.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);
}
}
}
这是要解析的example.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<books>
<book id="0001">
<title>802.11 Wireless Networks: The Definitive Guide, Second Edition </title>
<price>79.00</price>
<year>2007</year>
<isbn>9787564110062</isbn>
<author>
<name>Matthew S.Gast </name>
<introduce>无线网络规划和部署方面的权威作者</introduce>
</author>
<translator>O'Reilly</translator>
<publisher> O'Reilly Media, Inc. </publisher>
</book>
<book id="0002">
<title>TCP/IP Illustracted Volume 1:The Protocols </title>
<price>45.00</price>
<year>2004</year>
<isbn>7111075668</isbn>
<author>
<name>W.Richard Stevens </name>
<introduce>国际知名的Unix和网络专家</introduce>
</author>
<translator>范建华</translator>
<publisher>Addison Wesley/Pearson </publisher>
</book>
</books>
解析完之后,输出节点顺序错乱,并且还无法得到属性节点和属性值,不知道应该在原来的程序基础上怎样修改?(即将所有节点,包括属性节点和属性值按原xml文档顺序解析出来)
出现上面问题的原因是上面的程序逻辑有点问题,属性在代码中未输出,迭代的位置不对,如下是修改后的正确代码:
Java code:
import org.jdom.*;
import org.jdom.input.SAXBuilder;
import java.io.IOException;
import java.util.*;
public class Cute2 {
public static void main(String args[]){
//String url = "D:\\netbeanssy\\Jdom\\src\\jdom\\example.xml";
String url = "G:\\example.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);
}
}
}
}
import org.jdom.*;
import org.jdom.input.SAXBuilder;
import java.io.IOException;
import java.util.*;
public class Cute2{
public static void process(Element element){
List content=element.getContent();
Iterator iterator=content.iterator();
while(iterator.hasNext()){
Object o=iterator.next();
if(o instanceof Element){
Element child=(Element)o;
process(child);
}
}
String qualifiedName=element.getQualifiedName();
System.out.println(qualifiedName+":"+element.getText());
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();
}
}
}
public static void main(String[] args){
String url="D:\\netbeanssy\\Jdom\\src\\jdom\\example.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);
}
}
}
这是要解析的example.xml文件
<?xml version="1.0" encoding="UTF-8"?>
<books>
<book id="0001">
<title>802.11 Wireless Networks: The Definitive Guide, Second Edition </title>
<price>79.00</price>
<year>2007</year>
<isbn>9787564110062</isbn>
<author>
<name>Matthew S.Gast </name>
<introduce>无线网络规划和部署方面的权威作者</introduce>
</author>
<translator>O'Reilly</translator>
<publisher> O'Reilly Media, Inc. </publisher>
</book>
<book id="0002">
<title>TCP/IP Illustracted Volume 1:The Protocols </title>
<price>45.00</price>
<year>2004</year>
<isbn>7111075668</isbn>
<author>
<name>W.Richard Stevens </name>
<introduce>国际知名的Unix和网络专家</introduce>
</author>
<translator>范建华</translator>
<publisher>Addison Wesley/Pearson </publisher>
</book>
</books>
解析完之后,输出节点顺序错乱,并且还无法得到属性节点和属性值,不知道应该在原来的程序基础上怎样修改?(即将所有节点,包括属性节点和属性值按原xml文档顺序解析出来)
出现上面问题的原因是上面的程序逻辑有点问题,属性在代码中未输出,迭代的位置不对,如下是修改后的正确代码:
Java code:
import org.jdom.*;
import org.jdom.input.SAXBuilder;
import java.io.IOException;
import java.util.*;
public class Cute2 {
public static void main(String args[]){
//String url = "D:\\netbeanssy\\Jdom\\src\\jdom\\example.xml";
String url = "G:\\example.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);
}
}
}
}
此文由Web开发之答疑解惑源整理而成,若需转载,请注明原文出处:解决jdom遍历xml时输出节点顺序错误的问题(http://www.znjcx.com/html/y2012/3512_solve-the-problem-of-jdom-output-when-traversing-the-xml-node-sequence-error.html),谢谢!
更多热门文章: