用到的那个javaBean: ParseExample.java:
public class ParseExample { String model[]= new String[100];
public ParseExample() {
}
public String[] getParse (String xfile){ File file = new File("C:/succ/"+xfile);
getParseModel(file);
return this.xmlmodel; } public void getParseModel(File file){
SAXReader reader=new SAXReader();
try {
Document doc=reader.read(file);
Element root=doc.getRootElement();
String rootName=root.getName();
List<Element> list=doc.selectNodes(rootName+"/*");
for(int i=0;i<list.size();i++){
Element element=(Element)list.get(i);
walkNodes(element);
}
} catch (DocumentException e) {
e.printStackTrace();
}
}
//递归解析
public void walkNodes(Element element){
for(int i=0;i<element.nodeCount();i++){
Node node=element.node(i);
System.out.println(node.getNodeTypeName());
if(node instanceof Element){
walkNodes((Element)node);
}else{
String str=node.getText().trim();
if(null==str){
str="";
}
if(!"".equals(str)){
System.out.println("text is :"+str);
model[i]= str;
}
}
}
}
调用上面的Bean的JSP页面的部分代码: 。 。 。
if(checkFile.exists()){
String xInfor[]= new String[100];
xmodel=upload.getParse(saveFile);for(int i=0;i<100;i++)
{
out.println(xmodel[i]);
} 。
。
。
我执行了这个JSP页面后,显示的调用结果只有xInfor[0]不为空,且xInfor[0]=xmodel[]的最后一个元素的值;谁知道应该怎么改才能把xmodel[]的元素值全部返回给 xInfor[]呀???
public class ParseExample { String model[]= new String[100];
public ParseExample() {
}
public String[] getParse (String xfile){ File file = new File("C:/succ/"+xfile);
getParseModel(file);
return this.xmlmodel; } public void getParseModel(File file){
SAXReader reader=new SAXReader();
try {
Document doc=reader.read(file);
Element root=doc.getRootElement();
String rootName=root.getName();
List<Element> list=doc.selectNodes(rootName+"/*");
for(int i=0;i<list.size();i++){
Element element=(Element)list.get(i);
walkNodes(element);
}
} catch (DocumentException e) {
e.printStackTrace();
}
}
//递归解析
public void walkNodes(Element element){
for(int i=0;i<element.nodeCount();i++){
Node node=element.node(i);
System.out.println(node.getNodeTypeName());
if(node instanceof Element){
walkNodes((Element)node);
}else{
String str=node.getText().trim();
if(null==str){
str="";
}
if(!"".equals(str)){
System.out.println("text is :"+str);
model[i]= str;
}
}
}
}
调用上面的Bean的JSP页面的部分代码: 。 。 。
if(checkFile.exists()){
String xInfor[]= new String[100];
xmodel=upload.getParse(saveFile);for(int i=0;i<100;i++)
{
out.println(xmodel[i]);
} 。
。
。
我执行了这个JSP页面后,显示的调用结果只有xInfor[0]不为空,且xInfor[0]=xmodel[]的最后一个元素的值;谁知道应该怎么改才能把xmodel[]的元素值全部返回给 xInfor[]呀???
所以总是只有一个值。实际上walkNodes是不需要的,因为XPath本身就可以直接找出所谓的文本节点,直接得到字符串数组。
那应该在哪里设计数器并累计增1呀??我好多年前学了点java,现在都忘光了。拜托您说的详细点,或者您把XPath的方法说一下?谢谢了。 反正我想实现的功能就是把xml中的字符串提取出来再返回给调用的JSP页面,只要能实现这个功能就行,方法越简单,也容易懂更好。
我要提取字符串的xml文件格式如下: <?xml version="1.0" encoding="UTF-8" ?> <Student_Info>
<Student_Name>Zhoutongtong</Student_Name>
<Address>futian</Address>
<City>Guangzhou</City>
<Province>Guangdong</Province>
<Country>China</Country>
<School>shiyanxiaoxue</School>
<Contact_Info>
<First_Name>tony</First_Name>
<Last_Name>tang</Last_Name>
<Email>[email protected]</Email>
<Contact_First_Name>tongtong</Contact_First_Name>
<Contact_Last_Name>Zhou</Contact_Last_Name>
<Contact_Email>[email protected]</Contact_Email>
</Contact_Info>
<School_Info>
<School_Email>[email protected]</School_Email>
<General_Email>[email protected]</General_Email>
<School_Phone>137925</School_Phone>
<General_Phone>8313</General_Phone>
<Fax_Phone>0808</Fax_Phone>
</School_Info>
</School_Info>
int current = 0; //累积计数器
...
//递归解析
public void walkNodes(Element element){
for(int i=0;i<element.nodeCount();i++){
Node node=element.node(i);
System.out.println(node.getNodeTypeName());
if(node instanceof Element){
walkNodes((Element)node);
}else{
String str=node.getText().trim();
if(null==str){
str="";
}
if(!"".equals(str)){
System.out.println("text is :"+str);
model[current]= str;
current++;
}
}
}就是上面的这个意思。没有测试过,有小问题的话自己继续调调吧。