这几天在开发中遇到的问题
XML文档内容
1
<?
xml version="1.0" encoding="GB2312"
?>
2 < Result xmlns ="http://www.fiorano.com/fesb/activity/DBQueryOnInput2/Out" >
3 < query > select * from users where users_id='11111' </ query >
4 < row resultcount ="1" >
5 < column name ="users_id" type ="CHAR" > 11111 </ column >
6 < column name ="users_name" type ="CHAR" > wangwei </ column >
7 < column name ="users_group" type ="CHAR" > 80 </ column >
8 < column name ="users_address" type ="CHAR" > 11111号 </ column >
9 </ row >
10 < row resultcount ="1" >
11 < column name ="users_id" type ="CHAR" > 11111 </ column >
12 < column name ="users_name" type ="CHAR" > wangwei </ column >
13 < column name ="users_group" type ="CHAR" > 80 </ column >
14 < column name ="users_address" type ="CHAR" > 11111号 </ column >
15 </ row >
16 </ Result >
2 < Result xmlns ="http://www.fiorano.com/fesb/activity/DBQueryOnInput2/Out" >
3 < query > select * from users where users_id='11111' </ query >
4 < row resultcount ="1" >
5 < column name ="users_id" type ="CHAR" > 11111 </ column >
6 < column name ="users_name" type ="CHAR" > wangwei </ column >
7 < column name ="users_group" type ="CHAR" > 80 </ column >
8 < column name ="users_address" type ="CHAR" > 11111号 </ column >
9 </ row >
10 < row resultcount ="1" >
11 < column name ="users_id" type ="CHAR" > 11111 </ column >
12 < column name ="users_name" type ="CHAR" > wangwei </ column >
13 < column name ="users_group" type ="CHAR" > 80 </ column >
14 < column name ="users_address" type ="CHAR" > 11111号 </ column >
15 </ row >
16 </ Result >
本人遇到的问题是在<row></row>下的子元素名称相同如上第5,6,7,8行.这时当用JDOM通过如下代码获取时
package
com.xml;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.Namespace;
import org.jdom.input.SAXBuilder;
public class DuXMLTest2 {
public void loadXMLdoc(){
FileInputStream fi = null;
try {
fi = new FileInputStream("Test2.xml");
SAXBuilder sb = new SAXBuilder();
try {
Document doc = sb.build(fi);
Element root = doc.getRootElement();
Namespace ns = root.getNamespace();
List jiedian = root.getChildren();
Element et = (Element) jiedian.get(1);
//System.out.println(et.getName());
List zjiedian = et.getChildren();
Element xet = null;
String value[] = new String[zjiedian.size()];
for(int i=0;i<jiedian.size()-1;i++){
xet = (Element) jiedian.get(i+1);
for(int j=0;j<zjiedian.size();j++){
value[j]=xet.getChild("column",ns).getText();
System.out.println(value[j]);
}
}
} catch (JDOMException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
public static void main(String[] args){
DuXMLTest2 dt = new DuXMLTest2();
dt.loadXMLdoc();
}
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.Namespace;
import org.jdom.input.SAXBuilder;
public class DuXMLTest2 {
public void loadXMLdoc(){
FileInputStream fi = null;
try {
fi = new FileInputStream("Test2.xml");
SAXBuilder sb = new SAXBuilder();
try {
Document doc = sb.build(fi);
Element root = doc.getRootElement();
Namespace ns = root.getNamespace();
List jiedian = root.getChildren();
Element et = (Element) jiedian.get(1);
//System.out.println(et.getName());
List zjiedian = et.getChildren();
Element xet = null;
String value[] = new String[zjiedian.size()];
for(int i=0;i<jiedian.size()-1;i++){
xet = (Element) jiedian.get(i+1);
for(int j=0;j<zjiedian.size();j++){
value[j]=xet.getChild("column",ns).getText();
System.out.println(value[j]);
}
}
} catch (JDOMException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
public static void main(String[] args){
DuXMLTest2 dt = new DuXMLTest2();
dt.loadXMLdoc();
}
}
通过上面的代码运行时产生的结果都只取属性name="users_id"的元素的值即显示的内容都为11111.看到这结果当时都快晕了,马上要吃中午饭搞出这么个问题. 哎先吃饭吧
谁叫"人是铁饭是钢呢".吃饭回来就开始在网上查资料找了半天没的.看来只有靠自己了,打开JDOM的API文档一个个方法往过看(E文我哭啊),最后终于找到了
在Element类下有一个getContent(
int
index)方法
public Content getContent( int index)其方法的意思是Returns the child at the given index.
然后我又打开Content 类在其中找到getValue() 方法
public abstract java.lang.String getValue()其方法的意思是Returns the XPath 1.0 string value of this child
public Content getContent( int index)其方法的意思是Returns the child at the given index.
然后我又打开Content 类在其中找到getValue() 方法
public abstract java.lang.String getValue()其方法的意思是Returns the XPath 1.0 string value of this child
看到这我兴奋了,不管三七二十一我就烧黄纸砍猪头想和getContent兄和getValue兄结为兄弟. 我对你们的崇拜有如滔滔江山....................... 啊!跑题了
从新更改过的代码
package
com.xml;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.Namespace;
import org.jdom.input.SAXBuilder;
public class DuXMLTest2 {
public void loadXMLdoc(){
FileInputStream fi = null;
try {
fi = new FileInputStream("Test2.xml");
SAXBuilder sb = new SAXBuilder();
try {
Document doc = sb.build(fi);
Element root = doc.getRootElement();
Namespace ns = root.getNamespace();
List jiedian = root.getChildren();
Element et = (Element) jiedian.get(1);
//System.out.println(et.getName());
List zjiedian = et.getChildren();
int x = zjiedian.size();
//System.out.println(x);
String value[] = new String[x];
Element xet = null;
for(int i=0;i<jiedian.size()-1;i++){
xet = (Element) jiedian.get(i+1);
//System.out.println(xet.getName());
for(int j=0;j<zjiedian.size();j++){
value[j]=xet.getContent(j).getValue();
System.out.print(value[j]);
}
}
} catch (JDOMException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
public static void main(String[] args){
DuXMLTest2 dt = new DuXMLTest2();
dt.loadXMLdoc();
}
}
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.Namespace;
import org.jdom.input.SAXBuilder;
public class DuXMLTest2 {
public void loadXMLdoc(){
FileInputStream fi = null;
try {
fi = new FileInputStream("Test2.xml");
SAXBuilder sb = new SAXBuilder();
try {
Document doc = sb.build(fi);
Element root = doc.getRootElement();
Namespace ns = root.getNamespace();
List jiedian = root.getChildren();
Element et = (Element) jiedian.get(1);
//System.out.println(et.getName());
List zjiedian = et.getChildren();
int x = zjiedian.size();
//System.out.println(x);
String value[] = new String[x];
Element xet = null;
for(int i=0;i<jiedian.size()-1;i++){
xet = (Element) jiedian.get(i+1);
//System.out.println(xet.getName());
for(int j=0;j<zjiedian.size();j++){
value[j]=xet.getContent(j).getValue();
System.out.print(value[j]);
}
}
} catch (JDOMException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
} catch (IOException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
} catch (FileNotFoundException e) {
// TODO 自动生成 catch 块
e.printStackTrace();
}
}
public static void main(String[] args){
DuXMLTest2 dt = new DuXMLTest2();
dt.loadXMLdoc();
}
}