Xpath功能

时间:2013-06-11 19:02:51

标签: java xpath

我有一个模块,它使用web服务并从中获取响应。我得到的响应包含一个值,我需要在其中设置它的值。我正在使用XPath。现在当我在本地系统上运行此功能时,我得到了所需的输出,但是当我在耳边使用它时,它无效。

耳中的代码:

private static String stResponsePath="//Envelope//Body//AddNoteResponse//Response//";
private static String Fault_Path="//Envelope/Body/Fault/detail/CSIApplicationException/ServiceProviderEntity/";

/**
 * Construct AddNoteResponse bean from 
 * 
 * @param addNote response SOAP XML
 */
public AddNoteResponse createAddNoteResponseFromSoapString(String soapString)throws EDDSystemException
{
    try
    {
        Document soapDocument =AddNoteDomParserHelper.convertStringToDoc(soapString);       
        // EDDLog.outLog.info("soapDocument " + soapDocument.getFirstChild());
        // EDDLog.outLog.info("soapDocument " + soapDocument.getLastChild());

        EDDLog.outLog.info("soapString :" +soapString );
        AddNoteResponse addNoteResponse= new AddNoteResponse();
        addNoteResponse.setCode(AddNoteDomParserHelper.**getElementValue**(stResponsePath + "code", soapDocument));
        addNoteResponse.setDescription(AddNoteDomParserHelper.**getElementValue**(stResponsePath + "description", soapDocument));
    } .....
      .....
}   

public static String **getElementValue**(String elementName, Document doc) throws Exception
    {
        String value = "";
        XPath xPath = XPathFactory.newInstance().newXPath();
        if(xPath ==null)
        {
            EDDLog.outLog.info(" xpath is  null");
        }
        EDDLog.outLog.info("doc " + doc.getFirstChild());
        NodeList docNodes = (NodeList) xPath.evaluate(elementName,doc, XPathConstants.NODESET);
        // NodeList docNodes = (NodeList) xPath.evaluate("/Envelope/Body/AddNoteResponse/Response/code",doc, XPathConstants.NODESET);   
        EDDLog.outLog.info("elementName :" +elementName );

        //NodeList docNodes = doc.getElementsByTagNameNS("*", elementName);
        if (docNodes == null || docNodes.getLength() == 0) {
            docNodes = doc.getElementsByTagName(elementName);
            EDDLog.outLog.info("docNodes.getLength() :"+docNodes.getLength());
        }

输出是:

11 Jun 2013 13:47:40,644 INFO   soapString :<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"><SOAP-ENV:Header> <MessageHeader xmlns:cng="http://csi.cingular.com/CSI/Nam
espaces/Types/Public/CingularDataModel.xsd" xmlns="http://csi.cingular.com/CSI/Namespaces/Types/Public/MessageHeader.xsd"><TrackingMessageHeader><cng:version>v68</cng:version><cng:originalVersion>v
68</cng:originalVersion><cng:messageId>46178</cng:messageId><cng:timeToLive>120000</cng:timeToLive><cng:conversationId>EDD~CNG-CSI~5bfa2f5f-103d-4056-b775-cb2028357a5c</cng:conversationId><cng:date
TimeStamp>2013-06-08T12:44:18.418Z</cng:dateTimeStamp><cng:uniqueTransactionId>ServiceGateway19121@q25csg1c1_4f771d65-41de-4c40-b4cc-0be179d3677c</cng:uniqueTransactionId></TrackingMessageHeader><S
ecurityMessageHeader><cng:userName>EDD</cng:userName><cng:userPassword>EDDtest</cng:userPassword></SecurityMessageHeader><SequenceMessageHeader><cng:sequenceNumber>1</cng:sequenceNumber><cng:totalInSequence>1</cng:totalInSequence></SequenceMessageHeader></MessageHeader></SOAP-ENV:Header> <SOAP-ENV:Body> <AddNoteResponse xmlns:cng="http://csi.cingular.com/CSI/Namespaces/Types/Public/CingularDataModel.xsd" xmlns="http://csi.cingular.com/CSI/Namespaces/Container/Public/AddNoteResponse.xsd"><Response><cng:code>0</cng:code><cng:description>Success</cng:description></Response></AddNoteResponse></SOAP-ENV:Body> </SOAP-ENV:Envelope>
11 Jun 2013 13:47:42,028 INFO   doc [SOAP-ENV:Envelope: null]
11 Jun 2013 13:47:44,236 INFO   elementName ://Envelope//Body//AddNoteResponse//Response//code
11 Jun 2013 13:47:44,236 INFO   docNodes.getLength() :0
11 Jun 2013 13:47:44,243 INFO   doc [SOAP-ENV:Envelope: null]
11 Jun 2013 13:47:44,254 INFO   elementName ://Envelope//Body//AddNoteResponse//Response//description
11 Jun 2013 13:47:44,254 INFO   docNodes.getLength() :0

但是当我在本地系统上尝试相同的代码时,它运行正常:

Entering .convertStringToDoc()...
Exiting .convertStringToDoc()...
soapDocument [SOAP-ENV:Envelope: null]
9
 xpath is not null
elementName :/Envelope/Body/AddNoteResponse/Response/code
docNodes length:1
value :0
docNodes length2:1
addNoteResponse.getCode
 xpath is not null
elementName :/Envelope/Body/AddNoteResponse/Response/description
docNodes length:1
value :Success
docNodes length2:1

这是我正在处理的回复:

<SOAP-ENV:Envelope >
    <SOAP-ENV:Header> 
        <MessageHeader >
            <TrackingMessageHeader>
                <cng:version>v68</cng:version>
                <cng:originalVersion>v68</cng:originalVersion>
                <cng:messageId>45266</cng:messageId>
                <cng:timeToLive>120000</cng:timeToLive>
                <cng:conversationId>xxx</cng:conversationId>
                <cng:dateTimeStamp>2013-06-05T10:01:41.09Z</cng:dateTimeStamp>
                <cng:uniqueTransactionId>xasas</cng:uniqueTransactionId>
            </TrackingMessageHeader>
            <SecurityMessageHeader>
                <cng:userName>ass</cng:userName>
                <cng:userPassword>asas</cng:userPassword>
            </SecurityMessageHeader>
            <SequenceMessageHeader>
                <cng:sequenceNumber>1</cng:sequenceNumber>
                <cng:totalInSequence>1</cng:totalInSequence>
            </SequenceMessageHeader>
        </MessageHeader>
    </SOAP-ENV:Header> 
    <SOAP-ENV:Body> 
        <AddNoteResponse >
            <Response>
                <cng:code>0</cng:code>
                <cng:description>Success</cng:description>
            </Response>
        </AddNoteResponse>
    </SOAP-ENV:Body>
</SOAP-ENV:Envelope>

我感兴趣的价值是:

<cng:code>0</cng:code>
<cng:description>Success</cng:description>

0 个答案:

没有答案