Java - 使用 DOM 解析 xml
我正在尝试解析以下 xml。我可以轻松访问 WeekNumber,但无法访问 EmployeeRatesLevelA 和 EmployeeRatesLevelB 的子级。目标是将它们保存到一个类、带有字段 WeekNumber 和 ArrayLists、EmployeeRatesLevelA 和 EmployeeRatesLevelB 的 DataSet。 谢谢。
<DataSet ActiveFrom="2011/04/06">
<WeekNumber>8</WeekNumber>
<EmployeeRatesLevelA>
<Rate>0</Rate>
<Rate>0.12</Rate>
</EmployeeRatesLevelA>
<EmployeeRatesLevelB>
<Rate>0.15</Rate>
<Rate>0.20</Rate>
</EmployeeRatesLevelB>
</DataSet>
Document doc = loadXml("data.xml");
NodeList nodeList = doc.getElementsByTagName("DataSet");
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;
NodeList weekNumberList = element.getElementsByTagName("WeekNumber");
Element weekElement = (Element) weekNumberList.item(0);
NodeList textElementList = weekElement.getChildNodes();
System.out.println("Weeknumber:"+ ((Node)textElementList.item(0)).getNodeValue().trim());
}
public static Document loadXml(String file) {
try {
return (DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File(file)));
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
return null;
}
这给了我周数,但我无法访问 EmployeeRatesLevelA 和 EmployeeRatesLevelB。
想要学习其他很酷的东西,但由于我是 Java 新手,而且 xml 文档非常小,DOM 应该足够了。
I am trying to parse the following xml. I can access the WeekNumber easily but cannot access the children for EmployeeRatesLevelA and EmployeeRatesLevelB. The goal is to save these to a class, DataSet with fields WeekNumber and ArrayLists, EmployeeRatesLevelA and EmployeeRatesLevelB.
Thanks.
<DataSet ActiveFrom="2011/04/06">
<WeekNumber>8</WeekNumber>
<EmployeeRatesLevelA>
<Rate>0</Rate>
<Rate>0.12</Rate>
</EmployeeRatesLevelA>
<EmployeeRatesLevelB>
<Rate>0.15</Rate>
<Rate>0.20</Rate>
</EmployeeRatesLevelB>
</DataSet>
Document doc = loadXml("data.xml");
NodeList nodeList = doc.getElementsByTagName("DataSet");
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;
NodeList weekNumberList = element.getElementsByTagName("WeekNumber");
Element weekElement = (Element) weekNumberList.item(0);
NodeList textElementList = weekElement.getChildNodes();
System.out.println("Weeknumber:"+ ((Node)textElementList.item(0)).getNodeValue().trim());
}
public static Document loadXml(String file) {
try {
return (DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File(file)));
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
return null;
}
This gives me the Weeknumber but I am unable to access EmployeeRatesLevelA and EmployeeRatesLevelB.
Would like to learn other cool stuff but as I am new to Java and the xml document is really small, DOM should suffice.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
正如评论中提到的,请向我们展示您的 Java 代码。您可能还需要考虑查看 JAXB——XML 绑定的 Java 架构。这专门用于将 XML 表示为 Java 对象。无论出于何种原因,它可能对您的解决方案都不可行,但一定要看看:
http://jaxb.java。 net/tutorial/
DataSet
下面的域对象是您在问题中描述的内容:
演示
以下代码演示了如何使用 JAXB 运行时将 XML 与域对象相互转换:
As mentioned in the comments, please show us your Java code. You may also want to consider looking at JAXB - the Java Architecture for XML Binding. This is specifically geared towards representing XML as Java objects. It may not be feasible for your solution for whatever reason, but definitely take a look:
http://jaxb.java.net/tutorial/
DataSet
The following domain object below is what you described in your question:
Demo
The following code demonstrates how to use the JAXB runtime to convert your XML to/from your domain objects:
如果您想使用 DOM,我建议您首先编写一些帮助器类,以使您的工作更轻松。这是我最近写的一篇供我个人使用的文章。
让我们从包中的帮助器类开始 xml.utils
XmlException.java
XmlDocument.java
XmlNode.java
现在DataSet.java和Main.java如下:
DataSet.java
Main.java
If you want to use DOM, I suggest you to start by writing some helper classes to make your work easier. Here is one I written recently for my personal use.
Let's start with the helper classes in package xml.utils
XmlException.java
XmlDocument.java
XmlNode.java
Now the DataSet.java and Main.java are as follows:
DataSet.java
Main.java
Element.getElementsByTagName("EmployeeRatesLevelA") 其中 Element 应该是 DataSet。或者你可以使用
http://java.sun.com /javase/6/docs/api/org/w3c/dom/Node.html#getChildNodes()
然后过滤所有子节点,直到找到所需的子节点。
Element.getElementsByTagName("EmployeeRatesLevelA") where Element should be DataSet. Or you can use
http://java.sun.com/javase/6/docs/api/org/w3c/dom/Node.html#getChildNodes()
then filter all the childs until you find the ones you want to.
我非常喜欢 groovy 的这些功能,特别是如果将内容加载到数据库中是一次性的:
http://groovy.codehaus.org/Reading+XML+using+Groovy%27s+XmlSlurper
I quite love groovy for these things, particularly if it's a one off to load stuff into a database:
http://groovy.codehaus.org/Reading+XML+using+Groovy%27s+XmlSlurper