如何使用 JxPath 和 DOM 解析器解析 XML 文件
How to parse XML file with JxPath and DOM parser
我需要一个简单的示例,说明如何使用 Java DOM 解析器和 Apache JxPath 解析 XML 文件。我知道 DOM 解析器技术,但现在我正在尝试将 JxPath 集成到我的源代码中。
我在网络上搜索过,但找不到工作示例。
为了测试,我得到了这个 xml:
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<cd gender="male">
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd gender="male">
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>
</catalog>
Java代码:
File file = new File("Files/catalog.xml");
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
try
{
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(file);
NodeList nList = doc.getElementsByTagName("cd");
for(int j = 0; j<nList.getLength(); j++){
Element element = (Element)nList.item(j);
System.out.println(element.getElementsByTagName("artist").item(0).getTextContent() + "\n");
}
}
catch (ParserConfigurationException | SAXException | IOException e)
{
e.printStackTrace();
}
我已经阅读了 类 Container、DocumentContainer 和 JXPathContext,但是
如果您能提供一些帮助或提供具有特定工作示例的网络资源,我将不胜感激。
这是与您的工作类似的示例。
File file = new File("Files/catalog.xml");
DocumentContainer dc = new DocumentContainer(file.toURI().toURL());
JXPathContext ctx = JXPathContext.newContext(dc);
Iterator iter = ctx.iterate("//cd/artist");
//In this case, following API will return DOM object
//Iterator iter = ctx.selectNodes("//cd/artist").iterator();
while (iter.hasNext()) {
System.out.println(iter.next());//object type is String
}
您确定 JXPath 是您需要的吗? JXPath 是一个 Apache 库,用于在不一定 XML 的事物上使用 XPath 表达式,例如 Java 对象树或地图。如果您从 XML 创建了一个 DOM,您也可以在其上使用默认的 Java XPath 实现。参见此处:https://docs.oracle.com/javase/7/docs/api/javax/xml/xpath/XPathFactory.html。事实上,JXpath 在这里对你不利,因为 DOM 有一个像 Element
和 Text
这样的对象树,带有元素名称这样的元数据。所以你得到的不是像 //cd/artist
这样的表达式,而是像 //*[@tagName='cd']/childNodes[@tagName='artist']
.
这样的表达式
现在,如果您出于某种原因必须能够在可能是 Java 对象树或 DOM 的对象上使用 XPath 表达式,而您并不知道 up-front,在那种情况下,JXPath 将是一个很好的 use-case,就像 beckyang 在他们的回答中描述的那样:通过使用 JXPath DocumentContainer 来访问文档。
是的,我花了几个小时阅读了 JxPath,现在我理解了这个 API
的逻辑
非常感谢您的回答。这是我的一些代码
public class Main {
private final static Logger log = Logger.getLogger(Main.class);
public static void main(String[] args) {
PropertyConfigurator.configure("log4j.properties");
File file = new File("students.xml");
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
try
{
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(file);
NodeList nList = doc.getElementsByTagName("cd");
for(int j = 0; j<nList.getLength(); j++){
Element element = (Element)nList.item(j);
JXPathContext context = JXPathContext.newContext(element);
log.debug(context.getValue("company/address/city[2]"));
}
}
catch (ParserConfigurationException | SAXException | IOException e)
{
e.printStackTrace();
}
}
我需要一个简单的示例,说明如何使用 Java DOM 解析器和 Apache JxPath 解析 XML 文件。我知道 DOM 解析器技术,但现在我正在尝试将 JxPath 集成到我的源代码中。
我在网络上搜索过,但找不到工作示例。
为了测试,我得到了这个 xml:
<?xml version="1.0" encoding="UTF-8"?>
<catalog>
<cd gender="male">
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd gender="male">
<title>Hide your heart</title>
<artist>Bonnie Tyler</artist>
<country>UK</country>
<company>CBS Records</company>
<price>9.90</price>
<year>1988</year>
</cd>
</catalog>
Java代码:
File file = new File("Files/catalog.xml");
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
try
{
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(file);
NodeList nList = doc.getElementsByTagName("cd");
for(int j = 0; j<nList.getLength(); j++){
Element element = (Element)nList.item(j);
System.out.println(element.getElementsByTagName("artist").item(0).getTextContent() + "\n");
}
}
catch (ParserConfigurationException | SAXException | IOException e)
{
e.printStackTrace();
}
我已经阅读了 类 Container、DocumentContainer 和 JXPathContext,但是 如果您能提供一些帮助或提供具有特定工作示例的网络资源,我将不胜感激。
这是与您的工作类似的示例。
File file = new File("Files/catalog.xml");
DocumentContainer dc = new DocumentContainer(file.toURI().toURL());
JXPathContext ctx = JXPathContext.newContext(dc);
Iterator iter = ctx.iterate("//cd/artist");
//In this case, following API will return DOM object
//Iterator iter = ctx.selectNodes("//cd/artist").iterator();
while (iter.hasNext()) {
System.out.println(iter.next());//object type is String
}
您确定 JXPath 是您需要的吗? JXPath 是一个 Apache 库,用于在不一定 XML 的事物上使用 XPath 表达式,例如 Java 对象树或地图。如果您从 XML 创建了一个 DOM,您也可以在其上使用默认的 Java XPath 实现。参见此处:https://docs.oracle.com/javase/7/docs/api/javax/xml/xpath/XPathFactory.html。事实上,JXpath 在这里对你不利,因为 DOM 有一个像 Element
和 Text
这样的对象树,带有元素名称这样的元数据。所以你得到的不是像 //cd/artist
这样的表达式,而是像 //*[@tagName='cd']/childNodes[@tagName='artist']
.
现在,如果您出于某种原因必须能够在可能是 Java 对象树或 DOM 的对象上使用 XPath 表达式,而您并不知道 up-front,在那种情况下,JXPath 将是一个很好的 use-case,就像 beckyang 在他们的回答中描述的那样:通过使用 JXPath DocumentContainer 来访问文档。
是的,我花了几个小时阅读了 JxPath,现在我理解了这个 API
的逻辑非常感谢您的回答。这是我的一些代码
public class Main {
private final static Logger log = Logger.getLogger(Main.class);
public static void main(String[] args) {
PropertyConfigurator.configure("log4j.properties");
File file = new File("students.xml");
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
try
{
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(file);
NodeList nList = doc.getElementsByTagName("cd");
for(int j = 0; j<nList.getLength(); j++){
Element element = (Element)nList.item(j);
JXPathContext context = JXPathContext.newContext(element);
log.debug(context.getValue("company/address/city[2]"));
}
}
catch (ParserConfigurationException | SAXException | IOException e)
{
e.printStackTrace();
}
}