2017-12-01 39 views
0

から空の値を返します。それは文字列です、私はxmlにそれを解析することができませんDocumentそれは時間がかかりすぎるので。 だから、私はこのstringを持っていると私はこのようにそれにXPathを使用します。XPathは、私は大きなXMLを持つ文字列のXML

String field = "/SomeRequest/Payment/cardNumber/text()"; 
XPathExpression compile = XPATH_FACTORY.newXPath().compile(field); 
String value = compile.evaluate(new InputSource(new StringReader(request))); //request is xml string 

を、それを行った後、valueは空です。私はxml Document(それを解析する)でそれをやろうとし、それが働いた。しかし今、私は簡単にそれをしたいですString。 この文字列で正規表現を使用したくないのは、多くの問題が生じる可能性があるからです。 XPathを使用してこの値をcardNumberにするにはどうすればよいですか?

答えて

-1

toStringDocumentで使用できます。あなたは正しい道にある

+1

はありません、それは解決策ではありません動作します。 – allocer

1

は、

public class XpathTest { 
    private XPathFactory XPATH_FACTORY = new XPathFactoryImpl(); 

    private String request = "<SomeRequest>\n" + 
      "<Reservation>\n" + 
      "...\n" + 
      "</Reservation>\n" + 
      "<Payment>\n" + 
      "<cardNumber>123</cardNumber>\n" + 
      "</Payment>\n" + 
      "</SomeRequest>"; 

    @Test 
    public void name() throws Exception { 
     String field = "/SomeRequest/Payment/cardNumber/text()"; 
     XPathExpression compile = XPATH_FACTORY.newXPath().compile(field); 
     String value = compile.evaluate(new InputSource(new StringReader(request))); 
     assertThat(value, equalTo("123")); 
    } 
} 
+0

しかし、それは動作しません私のコードのように見えます。 – allocer

+0

@allocerパスを確認してください。 "SomeRequest"はルートタグですか? –

+0

はい、そうです。私は 'String field ="/SomeRequest/Payment/cardNumber/text() ";' – allocer

関連する問題