[英]Parse XML document in java
I want to find the @name
from table tag, from given col_alt
. 我想从给定
col_alt
表标签中找到@name
。 Can anybody help me with this. 谁能帮我这个忙。 Is it possible with
javax.XML.Xpath
? javax.XML.Xpath
是否可能? If not can you suggest the other way to find this. 如果没有,您可以建议其他方法来找到它。
This is the example of XML data: 这是XML数据的示例:
<?xml version="1.0" encoding="UTF-8"?>
<mapping>
<table name="CLIENT">
<table_column>
<column name="CLIENT_NAME">
<col_alt>CLIENT_NAME</col_alt>
<col_alt>CLIENT NAME</col_alt>
<col_alt>NAME</col_alt>
<col_alt>NAMES</col_alt>
<col_alt>CUST NAME</col_alt>
<col_alt>CUSTOMER NAME</col_alt>
</column>
<column name="CLIENT_ADDRESS">
<col_alt>ADDRESS</col_alt>
<col_alt>LIVES IN</col_alt>
<col_alt>LOCATION</col_alt>
<col_alt>STAY</col_alt>
</column>
<column name="CLIENT_CONTACT_NO">
<col_alt>CONTACT NO</col_alt>
<col_alt>CONTACT</col_alt>
</column>
</table_column>
</table>
<table name="SALES_MASTER">
<table_column>
<column name="SALE_DATE">
<col_alt>SALE DATE</col_alt>
<col_alt>BILL DATE</col_alt>
<col_alt>SALES DATE</col_alt>
</column>
<column name="TOTAL_BILL">
<col_alt>TOTAL BILL</col_alt>
<col_alt>GRAND TOTAL</col_alt>
</column>
</table_column>
</table>
</mapping>
If i remember correctly Xpath expression like this should work for you. 如果我没有记错的话,这样的Xpath表达式应该适合您。 I do not have complete code to give it a try.
我没有完整的代码尝试一下。
XPathExpression expr = xpath.compile("//table/table_column/column[col_alt='SALE DATE']/@name");
Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println(nodes.item(i).getNodeValue());
}
doc - is the document reference of the xml. doc-是xml的文档参考。
Do you have a schema for the XML? 您有XML的架构吗? If so can you check JAXB
如果可以,您可以检查JAXB
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.