2016-08-03 8 views
0

私はセールスフォースから取得し、以下の文字列の応答からタグ値を取得しようとしています、我々はタグ<fullName>を持っていた上Javaを使用してSOAP応答から要素値を取り出す方法は?

<?xml version="1.0" encoding="UTF-8"?> 
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="http://soap.sforce.com/2006/04/metadata"> 
    <soapenv:Body> 
     <listMetadataResponse> 
     <result> 
      <createdById>00528000001m5RRAAY</createdById> 
      <createdByName>Hariprasath Thanarajah</createdByName> 
      <createdDate>1970-01-01T00:00:00.000Z</createdDate> 
      <fileName>objects/EmailMessage.object</fileName> 
      <fullName>EmailMessage</fullName> 
      <id /> 
      <lastModifiedById>00528000001m5RRAAY</lastModifiedById> 
      <lastModifiedByName>Hariprasath Thanarajah</lastModifiedByName> 
      <lastModifiedDate>1970-01-01T00:00:00.000Z</lastModifiedDate> 
      <namespacePrefix /> 
      <type>CustomObject</type> 
     </result> 
     </listMetadataResponse> 
    </soapenv:Body> 
</soapenv:Envelope> 

。私はタグ内の値を取得し、それをString配列に入れる必要があります。私は部分文字列メソッドで試してみましたが、1つの値しか返しません。誰も私にこれをするように提案することはできますか?

+0

使用[XPathの](http://docs.oracle.com/javase/8/docs/api/javax、文字列配列にそれらを入れて、以下のような文字列の配列にそれらを返すことができます/xml/xpath/package-summary.html)、または[DocumentBuilderの作成](http://docs.oracle.com/javase/8/docs/api/javax/xml/parsers/DocumentBuilderFactory.html#newDocumentBuilder-- )、文書にSOAPメッセージを解析し、[getElementsByTagNameの(http://docs.oracle.com/javase/8/docs/api/org/w3c/dom/Document.html#getElementsByTagName-java.langを使用します。文字列 - )。 – VGR

+0

私はこの方法で試しました。わたしにはできる。 – Hariprasath

答えて

1

Iは、以下のようなコード以上から

を試してみましたが、あなたはIDSのリストを取得します。その後、あなたは

List<String> output = getFullNameFromXml(response, "fullName"); 
String[] strarray = new String[output.size()]; 
output.toArray(strarray); 
System.out.print("Response Array is "+Arrays.toString(strarray)); 
0

あなたはちょうどここhttps://www.javacodegeeks.com/2013/05/parsing-xml-using-dom-sax-and-stax-parser-in-java.html説明するように、あなたがSAXまたはStAXのパーサーを使用することができ、この単一の要素を解析したい場合。

SAXParserFactory factory = SAXParserFactory.newInstance(); 
    SAXParser saxParser = factory.newSAXParser(); 

    DefaultHandler handler = new DefaultHandler() { 

    boolean fullName = false; 

    public void startElement(String uri, String localName,String qName, 
       Attributes attributes) throws SAXException { 

     System.out.println("Start Element :" + qName); 

     if (qName.equals("fullName")) { 
      fullName = true; 
     } 
    } 

    public void characters(char ch[], int start, int length) throws SAXException { 

     if (fullName) { 
      System.out.println("Full Name : " + new String(ch, start, length)); 
      fullName = false; 
     } 
    } 
} 
saxParser.parse(mySoapResponse, handler); 

また、Salesforce Webサービスを使用するSOAPクライアントを作成するには、JAX-WS APIの詳細を参照してください。

+0

他のウェブサイトは時間の経過と共に移動したり消えたりする傾向があるため、リンクのみの回答はお勧めしません。リンクした情報の概要や短い例を提供する方がよいでしょう。 – VGR

関連する問題