JDOM 解析器可用于读取 XML、解析 xml 并在更新内容后写入 XML 文件。
它将 JDOM2 文档存储在内存中以读取和修改它的值。
JDOM2 Maven 依赖
<dependency> <groupId>org.jdom</groupId> <artifactId>jdom2</artifactId> <version>2.0.6</version> </dependency>
要执行 XPath,我们还需要 jaxen。
<dependency> <groupId>jaxen</groupId> <artifactId>jaxen</artifactId> <version>1.1.6</version> </dependency>
创建 JDOM2 文档
我们可以使用下面列出的任何解析器创建 org.jdom2.Document
实例。
它们都解析 XML 并返回内存中的 JDOM 文档。
- 使用 DOM 解析器
private static Document getDOMParsedDocument(final String fileName) { Document document = null; try { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); //factory.setNamespaceAware(true); DocumentBuilder documentBuilder = factory.newDocumentBuilder(); org.w3c.dom.Document w3cDocument = documentBuilder.parse(fileName); document = new DOMBuilder().build(w3cDocument); } catch (IOException | SAXException | ParserConfigurationException e) { e.printStackTrace(); } return document; }
- 使用 SAX 解析器
private static Document getSAXParsedDocument(final String fileName) { SAXBuilder builder = new SAXBuilder(); Document document = null; try { document = builder.build(fileName); } catch (JDOMException | IOException e) { e.printStackTrace(); } return document; }
- 使用 StAX 解析器
private static Document getStAXParsedDocument(final String fileName) { Document document = null; try { XMLInputFactory factory = XMLInputFactory.newFactory(); XMLEventReader reader = factory.createXMLEventReader(new FileReader(fileName)); StAXEventBuilder builder = new StAXEventBuilder(); document = builder.build(reader); } catch (JDOMException | IOException | XMLStreamException e) { e.printStackTrace(); } return document; }
使用 XPath 读取 XML 内容
要使用 xpath 读取任何一组元素的值,我们需要编译 XPathExpression
并使用它的 evaluate()
方法。
String xmlFile = "employees.xml"; Document document = getSAXParsedDocument(xmlFile); XPathFactory xpfac = XPathFactory.instance(); //Read employee ids XPathExpression<Attribute> xPathA = xpfac.compile("//employees/employee/@id", Filters.attribute()); for (Attribute att : xPathA.evaluate(document)) { System.out.println("Employee Ids :: " + att.getValue()); } //Read employee first names XPathExpression<Element> xPathN = xpfac.compile("//employees/employee/firstName", Filters.element()); for (Element element : xPathN.evaluate(document)) { System.out.println("Employee First Name :: " + element.getValue()); }
更多: zhilu jiaocheng
读取和过滤 XML 内容
我们将使用下面的 employees.xml
文件进行测试。
<employees> <employee id="101"> <firstName>JackLi</firstName> <lastName>Li</lastName> <country>Netherlands</country> <department id="15"> <name>HR</name> </department> </employee> <employee id="102"> <firstName>BobRobert</firstName> <lastName>Ma</lastName> <country>USA</country> <department id="16"> <name>Man</name> </department> </employee> </employees>
读取根节点
使用document.getRootElement()
方法。
public static void main(String[] args) { String xmlFile = "employees.xml"; Document document = getSAXParsedDocument(xmlFile); Element rootNode = document.getRootElement(); System.out.println("Root Element :: " + rootNode.getName()); }
读取属性值
使用 Element.getAttributeValue() 方法。
public static void main(String[] args) { String xmlFile = "employees.xml"; Document document = getSAXParsedDocument(xmlFile); Element rootNode = document.getRootElement(); rootNode.getChildren("employee").forEach( ReadXMLDemo::readEmployeeNode ); } private static void readEmployeeNode(Element employeeNode) { //Employee Id System.out.println("Id : " + employeeNode.getAttributeValue("id")); }
读取元素值
使用 Element.getChildText()
或者 Element.getText()
方法。
public static void main(String[] args) { String xmlFile = "employees.xml"; Document document = getSAXParsedDocument(xmlFile); Element rootNode = document.getRootElement(); rootNode.getChildren("employee").forEach( ReadXMLDemo::readEmployeeNode ); } private static void readEmployeeNode(Element employeeNode) { //Employee Id System.out.println("Id : " + employeeNode.getAttributeValue("id")); //First Name System.out.println("FirstName : " + employeeNode.getChildText("firstName")); //Last Name System.out.println("LastName : " + employeeNode.getChildText("lastName")); //Country System.out.println("country : " + employeeNode.getChild("country").getText()); /**Read Department Content*/ employeeNode.getChildren("department").forEach( ReadXMLDemo::readDepartmentNode ); } private static void readDepartmentNode(Element deptNode) { //Department Id System.out.println("Department Id : " + deptNode.getAttributeValue("id")); //Department Name System.out.println("Department Name : " + deptNode.getChildText("name")); }
日期:2020-09-17 00:10:13 来源:oir作者:oir