方法里显示找不到前面定义的变量

轻叹无音丶 2014-12-29 09:50:59

方法里就是找不到有这个变量。
源代码:

package JDOM;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;

import jeff.zhan.entity.Book;

import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;

public class JDOM_AnalysisDemo {
private ArrayList<Book> bookListss= new ArrayList<Book>();
private String ssss;
public static void main(String[] args) {
// TODO Auto-generated method stub

// 1.创建一个SAXBuilder对象
SAXBuilder saxBuilder = new SAXBuilder();
InputStream in;
try {
// 2.创建一个输入流,将XML文件加载到输入流中
in = new FileInputStream("src/res/books.xml");
// 3.通过saxBuilder.builder方法,将输入流加载到saxbuilder中
Document document = saxBuilder.build(in);
// 4.通过document对象获取xml文件根节点
Element rootElement = document.getRootElement();
// 5.获取根节点下子节点的List集合(包括了所有<book></book>)
List<Element> bookList =rootElement.getChildren();
// 6.继续解析,进行for循环遍历
for(Element book:bookList){
//利用javabean存储Book对象结构
Book bookEntity = new Book();
System.out.println("=============开始解析第"
+ (bookList.indexOf(book) + 1) + "本书=============");
// 7.解析book的属性集合
List<Attribute> attrList = book.getAttributes();
// 知道节点下名字,获取值得方法
// book.getAttributeValue("id");
// 遍历book属性(针对不清楚book节点下属性名和值)
for(Attribute attr:attrList){
// 获取属性名称
String attrName = attr.getName();
// 获取属性值
String attrValue =attr.getValue();
System.out.println("属性名:"+attrName+"----属性值:"+attrValue);
if(attr.getName().equals("id")){
// 存储book结构中的id
bookEntity.setId(attr.getValue());
}
}
// 对book节点的子节点的节点名和节点值进行遍历
List<Element> bookChilds = book.getChildren();
for(Element child: bookChilds){
System.out.println("节点名:" + child.getName() + "----------"
+ "节点值:" + child.getValue());
if(child.getName().equals("name")){
bookEntity.setName(child.getValue());
}else if(child.getName().equals("author")){
bookEntity.setAuthor(child.getValue());
}else if(child.getName().equals("year")){
bookEntity.setYear(child.getValue());
}else if(child.getName().equals("price")){
bookEntity.setPrice(child.getValue());
}else if(child.getName().equals("language")){
bookEntity.setLanguage(child.getValue());
}
}
System.out.println("=============结束解析第"
+ (bookList.indexOf(book) + 1) + "本书============="+"\n");

bookEntity = null;
}

} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JDOMException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}
...全文
105 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhuyf87 2014-12-30
  • 打赏
  • 举报
回复
private static String s; 如果是non-static的成员变量,需要通过对象才能访问。
  • 打赏
  • 举报
回复
静态方法不能访问非静态的成员变量
轻叹无音丶 2014-12-29
  • 打赏
  • 举报
回复
多谢指教。
疯狂熊猫人 2014-12-29
  • 打赏
  • 举报
回复
朋友,静态方法是不能够引用非静态的属性或者方法的! main方法是static的,而你申明的两个属性都是非静态的,所以提示你找不到属性!

81,091

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧