大家好,我在写一个java读xml得程序,
一个book.xml得文件
<?xml version="1.0" encoding="UTF-8"?>
<book>
<person>
<first>Kiran</first>
<last>Pai</last>
<age>22</age>
</person>
<person>
<first>Bill</first>
<last>Gates</last>
<age>46</age>
</person>
</book>
---------------------------
意图:我想动态获取节点名放到数组里;
问题:怎样获取节点名;
部分代码:
public Vector getXml() throws IOException, SAXException,
ParserConfigurationException {
Vector head_Vector=new Vector();//Table's Title数组
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse(new File("book.xml"));//选中要读的xml
.......原来我是把读xml得代码写死了得,现在想写活,怎么遍历
...........................................
return head_Vector;
}
谢谢大家,我急用
一个book.xml得文件
<?xml version="1.0" encoding="UTF-8"?>
<book>
<person>
<first>Kiran</first>
<last>Pai</last>
<age>22</age>
</person>
<person>
<first>Bill</first>
<last>Gates</last>
<age>46</age>
</person>
</book>
---------------------------
意图:我想动态获取节点名放到数组里;
问题:怎样获取节点名;
部分代码:
public Vector getXml() throws IOException, SAXException,
ParserConfigurationException {
Vector head_Vector=new Vector();//Table's Title数组
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse(new File("book.xml"));//选中要读的xml
.......原来我是把读xml得代码写死了得,现在想写活,怎么遍历
...........................................
return head_Vector;
}
谢谢大家,我急用
private String show;
private String title;
private String author;
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getShow() {
return show;
}
public void setShow(String show) {
this.show = show;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
}
<?xml version="1.0" encoding="GBK"?> <books>
<!--第一次写xml文件-->
<book show="yes">
<title>java </title>
<author>张三 </author>
</book>
<book show="no">
<title>c# </title>
<author>李四 </author>
</book>
<book show="yes">
<title>sql bb </title>
<author>王五 </author>
</book>
<owner>beibei </owner>
</books>
//解析XML 文件,获取里面的值
public List getInfo()
{
List booklist=new ArrayList();
try {
SAXReader saxreader=new SAXReader();
Document document=saxreader.read(new File("f:\\bookxml.xml"));
List list=document.selectNodes("/books/book");
Iterator iter=list.iterator();
while(iter.hasNext())
{
BookTest book=new BookTest();
Element bookelement=(Element) iter.next();
book.setShow(bookelement.attributeValue("show"));
Element title=bookelement.element("title");
book.setTitle(title.getText());
Element author=bookelement.element("author");
book.setAuthor(author.getText());
booklist.add(book);
}
} catch (DocumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return booklist;
}
List items = null;
FileInputStream fis = null;
try {
items = new ArrayList();
fis = new FileInputStream();
SAXBuilder sb = new SAXBuilder();
Document doc = sb.build(fis);
Element root = doc.getRootElement(); // 得到根元素
items = root.getChildren("book"); // 得到根元素所有子元素的集合
}
2. 如果你的xml结构不变,而你是为了拿到每个节点的值,建议用Castor
----------------
fist last age ----表头..................----值