Java 解析 XML <Property> 标签示例:获取 Expression、Expression1 和 EntPeCalcItemView 内容
使用 Java 解析 XML <Property> 标签示例:获取 Expression、Expression1 和 EntPeCalcItemView 内容
本文将提供一个使用 Java 代码解析 XML <Property> 标签的示例,并详细介绍如何获取 Expression、Expression1 和 EntPeCalcItemView 节点的值,以及如何遍历 EntPeCalcItemView 节点列表。
示例 XML 文档:
<Property xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Expression>[科室与主诊组收入.科室总收入]/[科室与主诊组收入.科室总收入]</Expression>
<Expression1>{0}/{1}</Expression1>
<EntPeCalcItemView>
<Name>[科室与主诊组收入.科室总收入]</Name>
<TableId>112</TableId>
<Field>D001</Field>
<DataTimeType>0</DataTimeType>
<DataYear>0</DataYear>
<DataMonth>0</DataMonth>
<MonthType>0</MonthType>
<ValuType>0</ValuType>
</EntPeCalcItemView>
<EntPeCalcItemView>
<Name>[科室与主诊组收入.科室总收入]</Name>
<TableId>112</TableId>
<Field>D001</Field>
<DataTimeType>0</DataTimeType>
<DataYear>0</DataYear>
<DataMonth>0</DataMonth>
<MonthType>0</MonthType>
<ValuType>0</ValuType>
</EntPeCalcItemView>
<Round>-1</Round>
</Property>
Java 代码实现:
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
public class PropertyParser {
public static void main(String[] args) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse("property.xml");
// 获取根节点
Element root = doc.getDocumentElement();
// 获取 Expression 和 Expression1 节点的值
String expression = root.getElementsByTagName("Expression").item(0).getTextContent();
String expression1 = root.getElementsByTagName("Expression1").item(0).getTextContent();
// 获取 EntPeCalcItemView 节点列表
NodeList itemViews = root.getElementsByTagName("EntPeCalcItemView");
// 遍历 EntPeCalcItemView 节点列表,获取每个节点的 Name 值
for (int i = 0; i < itemViews.getLength(); i++) {
Element itemView = (Element) itemViews.item(i);
String name = itemView.getElementsByTagName("Name").item(0).getTextContent();
System.out.println(name);
}
// 获取 Round 节点的值
int round = Integer.parseInt(root.getElementsByTagName("Round").item(0).getTextContent());
System.out.println("Expression: " + expression);
System.out.println("Expression1: " + expression1);
System.out.println("Round: " + round);
} catch (Exception e) {
e.printStackTrace();
}
}
}
代码说明:
-
**导入必要的类:**代码首先导入 DOM 解析库相关的类,包括
DocumentBuilderFactory、DocumentBuilder、Document、Element和NodeList。 -
**解析 XML 文件:**代码使用
DocumentBuilderFactory和DocumentBuilder创建一个Document对象,并使用parse()方法解析名为property.xml的 XML 文件。 -
**获取根节点:**代码使用
getDocumentElement()方法获取 XML 文档的根节点,即<Property>标签。 -
**获取 Expression 和 Expression1 节点的值:**代码使用
getElementsByTagName()方法获取Expression和Expression1标签,并使用getTextContent()方法获取其文本内容。 -
**获取 EntPeCalcItemView 节点列表:**代码使用
getElementsByTagName()方法获取所有EntPeCalcItemView标签,并存储到NodeList对象中。 -
**遍历 EntPeCalcItemView 节点列表:**代码使用
for循环遍历NodeList对象,获取每个EntPeCalcItemView节点的Name值。 -
**获取 Round 节点的值:**代码使用
getElementsByTagName()方法获取Round标签,并使用getTextContent()方法获取其文本内容,并使用Integer.parseInt()方法将其转换为整数。
注意:
- 代码假设 XML 文件名为
property.xml,需要根据实际情况修改文档名。 - 代码仅展示了如何获取 XML 文档中一些节点和属性值,需要根据实际情况对代码进行修改以进行进一步处理。
原文地址: https://www.cveoy.top/t/topic/nnpX 著作权归作者所有。请勿转载和采集!