使用Java中的xPath获取节点

时间:2017-05-18 12:46:16

标签: java xml xpath

我需要在描述为T1时获取值28.42,并且从xPath列表中获取作业描述(Mecánica,Guarnecidos,...)的每个值。

<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>

我正在尝试获取列表,但节点是相同的。

     for (int i = 0; i < nodes.getLength(); i++) {
            Element pieza = (Element) nodes.item(i);            
            System.out.println(pieza.getNodeName());}

1 个答案:

答案 0 :(得分:0)

因为您需要两个详细信息:job/descriptionvalue,您需要两个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();
        }

    }
}

将xml放在名为pricebyhour.xml的文件中并运行此代码。

请注意,第一个xpath提取作业描述和第二个xpath提取值。 作业描述和值的数量应该匹配,您实际上可以构建一个包含两个值作业描述和值的类,如类PriceDetails所示。

输出是:

Job description is : Mecánica, Value is : 28.42
Job description is : Guarnecidos, Value is : 28.42