- Java XML 教程
- Java XML 主页
- Java XML 概述
- Java XML 解析器
- Java DOM 解析器
- Java DOM 解析器
- 解析XML文档
- 查询XML文档
- 创建 XML 文档
- 修改XML文档
- Java SAX 解析器
- Java SAX 解析器
- 解析XML文档
- 查询XML文档
- 创建 XML 文档
- 修改XML文档
- JDOM XML 解析器
- JDOM XML 解析器
- 解析XML文档
- 查询XML文档
- 创建 XML 文档
- 修改XML文档
- Java StAX 解析器
- Java StAX 解析器
- 解析XML文档
- 查询XML文档
- 创建 XML 文档
- 修改XML文档
- Java XPath 解析器
- Java XPath 解析器
- 解析XML文档
- 查询XML文档
- 创建 XML 文档
- 修改XML文档
- Java DOM4J 解析器
- Java DOM4J 解析器
- 解析XML文档
- 查询XML文档
- 创建 XML 文档
- 修改XML文档
- Java XML 有用资源
- Java XML - 问题与解答
- Java XML - 快速指南
- Java XML - 有用的资源
- Java XML - 讨论
Java JDOM 解析器 - 查询 XML 文档
演示示例
这是我们需要查询的输入 xml 文件 -
<?xml version = "1.0"?> <cars> <supercars company = "Ferrari"> <carname type = "formula one">Ferarri 101</carname> <carname type = "sports car">Ferarri 201</carname> <carname type = "sports car">Ferarri 301</carname> </supercars> <supercars company = "Lamborgini"> <carname>Lamborgini 001</carname> <carname>Lamborgini 002</carname> <carname>Lamborgini 003</carname> </supercars> <luxurycars company = "Benteley"> <carname>Benteley 1</carname> <carname>Benteley 2</carname> <carname>Benteley 3</carname> </luxurycars> </cars>
QueryXmlFileDemo.java
import java.io.File; import java.io.IOException; import java.util.List; import org.jdom2.Attribute; import org.jdom2.Document; import org.jdom2.Element; import org.jdom2.JDOMException; import org.jdom2.input.SAXBuilder; public class QueryXmlFileDemo { public static void main(String[] args) { try { File inputFile = new File("input.txt"); SAXBuilder saxBuilder = new SAXBuilder(); Document document = saxBuilder.build(inputFile); System.out.println("Root element :" + document.getRootElement().getName()); Element classElement = document.getRootElement(); List<Element> supercarList = classElement.getChildren("supercars"); System.out.println("----------------------------"); for (int temp = 0; temp < supercarList.size(); temp++) { Element supercarElement = supercarList.get(temp); System.out.println("\nCurrent Element :" + supercarElement.getName()); Attribute attribute = supercarElement.getAttribute("company"); System.out.println("company : " + attribute.getValue() ); List<Element> carNameList = supercarElement.getChildren("carname"); for (int count = 0; count < carNameList.size(); count++) { Element carElement = carNameList.get(count); System.out.print("car name : "); System.out.println(carElement.getText()); System.out.print("car type : "); Attribute typeAttribute = carElement.getAttribute("type"); if(typeAttribute != null) System.out.println(typeAttribute.getValue()); else { System.out.println(""); } } } } catch(JDOMException e) { e.printStackTrace(); } catch(IOException ioe) { ioe.printStackTrace(); } } }
这将产生以下结果 -
Root element :cars ---------------------------- Current Element :supercars company : Ferrari car name : Ferarri 101 car type : formula one car name : Ferarri 201 car type : sports car car name : Ferarri 301 car type : sports car Current Element :supercars company : Lamborgini car name : Lamborgini 001 car type : car name : Lamborgini 002 car type : car name : Lamborgini 003 car type :