[英]Using Xpath in java to extract a value from a large xml file
I would like to extract a single value from a large xml file (part of a much larger xml project that I'm working on) 我想从一个大型xml文件中提取单个值(我正在处理的一个更大的xml项目的一部分)
So the xml structure is like this: 所以xml结构是这样的:
<m:Report>
<m:Messages>
<m:report_type>
<m:Report_Data> (last value)
<m:Data_Item> (must be specific type)
<m:Value> (want this)
After this all tags close properly
So right xpath query looks something like this: 因此正确的xpath查询如下所示:
XPathExpression expr = x.compile("//m:Report/m:Messages/m:Report_type/m:Report_Data[m:Data_Iten='PRICE'][last()]/m:Value/text()") ;
I'm not having much luck with this query, where am I going wrong? 我在此查询中运气不佳,我在哪里出错?
There are simply errors in your query. 您的查询中仅存在错误。
Report_type
should be report_type
and Data_Iten
should be Data_Item
: Report_type
应该是report_type
而Data_Iten
应该是Data_Item
:
XPathExpression expr = x.compile("//m:Report/m:Messages/m:report_type/m:Report_Data[m:Data_Item='PRICE'][last()]/m:Value/text()");
Assuming that you have a Data_Item
in your input data with PRICE
, then this should get you the <m:Value>
. 假设您的
PRICE
输入数据中有一个Data_Item
,那么这应该为您提供<m:Value>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.