在Java中使用xPath获取节点
仅当description为T1时,并且对于职务描述的每个值(Mecánica、Guarnecidos,…),我需要使用xPath从列表中获取值28.42在Java中使用xPath获取节点,java,xml,xpath,Java,Xml,Xpath,仅当description为T1时,并且对于职务描述的每个值(Mecánica、Guarnecidos,…),我需要使用xPath从列表中获取值28.42 <priceByHourList> <priceByHour locked="false"> <value> <job> <jobId>4</jobId> <description>
<priceByHourList>
<priceByHour locked="false">
<value>
<job>
<jobId>4</jobId>
<description>
<id>4</id>
<value>Mecánica</value>
</description>
</job>
<technician>
<technicianId>1</technicianId>
<description>T1</description>
</technician>
<value>28.42</value>
</value>
</priceByHour>
<priceByHour locked="false">
<value>
<job>
<jobId>4</jobId>
<description>
<id>4</id>
<value>Mecánica</value>
</description>
</job>
<technician>
<technicianId>2</technicianId>
<description>T2</description>
</technician>
<value>28.42</value>
</value>
</priceByHour>
<priceByHour locked="false">
<value>
<job>
<jobId>4</jobId>
<description>
<id>4</id>
<value>Mecánica</value>
</description>
</job>
<technician>
<technicianId>3</technicianId>
<description>T3</description>
</technician>
<value>28.42</value>
</value>
</priceByHour>
<priceByHour locked="false">
<value>
<job>
<jobId>1</jobId>
<description>
<id>1</id>
<value>Electricidad</value>
</description>
</job>
<technician>
<technicianId>2</technicianId>
<description>T2</description>
</technician>
<value>28.42</value>
</value>
</priceByHour>
<priceByHour locked="false">
<value>
<job>
<jobId>1</jobId>
<description>
<id>1</id>
<value>Electricidad</value>
</description>
</job>
<technician>
<technicianId>3</technicianId>
<description>T3</description>
</technician>
<value>28.42</value>
</value>
</priceByHour>
<priceByHour locked="false">
<value>
<job>
<jobId>5</jobId>
<description>
<id>5</id>
<value>Guarnecidos</value>
</description>
</job>
<technician>
<technicianId>1</technicianId>
<description>T1</description>
</technician>
<value>28.42</value>
</value>
</priceByHour>
4.
4.
梅卡尼卡
1.
T1
28.42
4.
4.
梅卡尼卡
2.
T2
28.42
4.
4.
梅卡尼卡
3.
T3
28.42
1.
1.
电工
2.
T2
28.42
1.
1.
电工
3.
T3
28.42
5.
5.
瓜内西多斯
1.
T1
28.42
我试图得到这个列表,但是节点是一样的
for (int i = 0; i < nodes.getLength(); i++) {
Element pieza = (Element) nodes.item(i);
System.out.println(pieza.getNodeName());}
for(int i=0;i
因为您需要两个详细信息:job/description
和value
您需要两个xpath表达式。您可以执行以下操作:
public class PriceByHour
{
private static class PriceDetails
{
String jobDesc;
String value;
}
public static void main( String... args )
{
try
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse( new File( ".//pricebyhour.xml" ) );
XPathFactory xPathFactory = XPathFactory.newInstance();
XPath xpath = xPathFactory.newXPath();
List<PriceDetails> priceDetailsList = new ArrayList<PriceDetails>();
XPathExpression jobExpr = xpath.compile( "//priceByHour/value[technician[./description='T1']]/job/description/value" );
XPathExpression valueExpr = xpath.compile( "//priceByHour/value[technician[./description='T1']]/value" );
Object exprEval = jobExpr.evaluate( doc, XPathConstants.NODESET );
NodeList jobNodes = null;
NodeList valueNodes = null;
if ( exprEval != null && exprEval instanceof NodeList )
{
jobNodes = (NodeList)exprEval;
}
exprEval = valueExpr.evaluate( doc, XPathConstants.NODESET );
if ( exprEval != null && exprEval instanceof NodeList )
{
valueNodes = (NodeList)exprEval;
}
if ( jobNodes != null && valueNodes != null && jobNodes.getLength() == valueNodes.getLength() )
{
for ( int i = 0 ; i < jobNodes.getLength(); i++ )
{
Node jobNode = jobNodes.item( i );
Node valueNode = valueNodes.item( i );
PriceDetails priceDetails = new PriceDetails();
priceDetails.jobDesc = jobNode.getTextContent();
priceDetails.value = valueNode.getTextContent();
priceDetailsList.add( priceDetails );
}
}
for ( PriceDetails priceDetails : priceDetailsList )
{
System.out.println( "Job description is : " + priceDetails.jobDesc + ", " + "Value is : " + priceDetails.value );
}
}
catch ( Exception e )
{
e.printStackTrace();
}
}
}
这是JSoup吗?您是否尝试过
//value[.//description=“T1”]/value/text()
?
Job description is : Mecánica, Value is : 28.42
Job description is : Guarnecidos, Value is : 28.42