解析特定元素DOM - Java [英] Parse Specific Elements DOM - Java
问题描述
我相信这是一个简单的问题,但我无法找到它的工作原理。
这是XML文件(来自www.w3schools.com):
<?xml version =1.0encoding =ISO-8859-1?>
<! - 由XMLSpy®编辑 - >
< bookstore>
< book category =烹饪>
< title lang =en>日常意大利语< / title>
< author> Giada De Laurentiis< / author>
< year> 2005< / year>
< price> 30.00< / price>
< / book>
< book category =children>
< title lang =en>哈利波特< / title>
< author> J K. Rowling< / author>
< year> 2005< / year>
< price> 29.99< / price>
< / book>
< book category =web>
< title lang =en> XQuery开始< / title>
< author> James McGovern< / author>
< author> Per Bothner< / author>
< author> Kurt Cagle< / author>
< author> James Linn< / author>
< author> Vaidyanathan Nagarajan< / author>
< year> 2003< / year>
< price> 49.99< / price>
< / book>
< book category =webcover =平装本>
< title lang =en>学习XML< / title>
< author> Erik T. Ray< / author>
< year> 2003< / year>
< price> 39.95< / price>
< / book>
< / bookstore>
正如你可以看到这本书XQuery Kick Start有不止一个作者。
但是我找不到一个方法来获取正确的作者数量。
这是我的代码:
public static void main(String argv [])throws ParserConfigurationException,SAXException,IOException {
文件fXmlFile =新文件(\books.xml);
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
文档doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement()。normalize();
System.out.println(Root element:+ doc.getDocumentElement()。getNodeName());
NodeList nList = doc.getElementsByTagName(book);
System.out.println(----------------------------); (int temp = 0; temp< nList.getLength(); temp ++){
Node nNode = nList.item(temp);
System.out.println(\\\
Current Element:+ nNode.getNodeName());
if(nNode.getNodeType()== Node.ELEMENT_NODE){
元素eElement =(Element)nNode;
System.out.println(Category:+ eElement.getAttribute(category));
System.out.println(Title:+ eElement.getElementsByTagName(title)。item(0).getTextContent());
System.out.println(作者:+ eElement.getElementsByTagName(author)。item(0).getTextContent());
System.out.println(Year:+ eElement.getElementsByTagName(year)。item(0).getTextContent());
System.out.println(Price:+ eElement.getElementsByTagName(price)。item(0).getTextContent());
}
}
但作为结果我只会得到一个作者:
根元素:书店
----------------- -----------
当前元素:book
Categoria do Livro:烹饪
Titulo:日常意大利语
作者:Giada De Laurentiis
Ano:2005
价格:30.00
当前元素:book
类别:Livro:children
Titulo:Harry Potter
作者:J K 。Rowling
Ano:2005
价格:29.99
当前元素:book
Categoria do Livro:web
Titulo:XQuery Kick Start
作者:James McGovern
Ano:2003
价格:49.99
当前元素:书
类别:Livro:web
Titulo:学习XML
作者:Erik T. Ray
Ano:2003
价格:39.95
有没有人知道一个很好的方法来获取正确的元素数量?
对于长长的问题,我不知道如何表达自己,所以我不得不粘贴在这里
*我是新来的DOM *
您正在获得第一个作者,因为您正在检索第一个项目的
item(0)
尝试迭代它们,因为可能有多个
for(int i = 0; i< eElement.getElementsByTagName (author)getLength(); i ++)
System.out.println(作者:+
eElement.getElementsByTagName(author)。item(i).getTextContent());
I believe this is a simple question but I am having trouble to find out how it works.
That's the XML file (from www.w3schools.com):
<?xml version="1.0" encoding="ISO-8859-1"?>
<!-- Edited by XMLSpy® -->
<bookstore>
<book category="cooking">
<title lang="en">Everyday Italian</title>
<author>Giada De Laurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="children">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="web">
<title lang="en">XQuery Kick Start</title>
<author>James McGovern</author>
<author>Per Bothner</author>
<author>Kurt Cagle</author>
<author>James Linn</author>
<author>Vaidyanathan Nagarajan</author>
<year>2003</year>
<price>49.99</price>
</book>
<book category="web" cover="paperback">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
As you can see the book XQuery Kick Start has more than one author. But I cant find a way to get the right number of authors. Thats my code:
public static void main(String argv[]) throws ParserConfigurationException, SAXException, IOException {
File fXmlFile = new File("\books.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("book");
System.out.println("----------------------------");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
System.out.println("\nCurrent Element :" + nNode.getNodeName());
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
System.out.println("Category : " + eElement.getAttribute("category"));
System.out.println("Title : " + eElement.getElementsByTagName("title").item(0).getTextContent());
System.out.println("Author : " + eElement.getElementsByTagName("author").item(0).getTextContent());
System.out.println("Year : " + eElement.getElementsByTagName("year").item(0).getTextContent());
System.out.println("Price : " + eElement.getElementsByTagName("price").item(0).getTextContent());
}
}
But as Result I'll be getting only one author:
Root element :bookstore
----------------------------
Current Element :book
Categoria do Livro : cooking
Titulo : Everyday Italian
Autor : Giada De Laurentiis
Ano : 2005
Price : 30.00
Current Element :book
Categoria do Livro : children
Titulo : Harry Potter
Autor : J K. Rowling
Ano : 2005
Price : 29.99
Current Element :book
Categoria do Livro : web
Titulo : XQuery Kick Start
Autor : James McGovern
Ano : 2003
Price : 49.99
Current Element :book
Categoria do Livro : web
Titulo : Learning XML
Autor : Erik T. Ray
Ano : 2003
Price : 39.95
Does anyone knows a good method to get the right number of elements? sorry about the long question, I didnt know how to express myself so I had to paste here *I'm new to DOM*
You're are getting the first author always, as you're retrieving the first item of the nodelist
getElementsByTagName("author").item(0)
Try iterating them, as there could be more than one
for (int i = 0; i < eElement.getElementsByTagName("author").getLength(); i++)
System.out.println("Author : " +
eElement.getElementsByTagName("author").item(i).getTextContent());
这篇关于解析特定元素DOM - Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!