2014-09-26 56 views
1

從SharePoint List SOAP請求返回某些字段時遇到一些麻煩。Jdom2 Sharepoint XML字段

這裏是XML:

<?xml version="1.0" encoding="UTF-8"?> 
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:soap1="http://schemas.microsoft.com/sharepoint/soap/"> 
    <soap:Header/> 
    <soap:Body> 
     <soap1:UpdateListItems> 
     <soap1:listName>69A3FFFA-782B-45D5-B776-2BE6D5645745</soap1:listName> 
     <soap1:updates> 
      <Batch OnError="Continue"> 
      <Method ID="1" Cmd="New"> 
       <Field Name="Title">New Item</Field> 
      </Method> 
      </Batch> 
     </soap1:updates> 
     </soap1:UpdateListItems> 
    </soap:Body> 
</soap:Envelope> 

我可以使用下面的代碼Jdom2抓住這樣的特定值:

  // set your name spaces. 
      Namespace soap = Namespace.getNamespace("soap","http://www.w3.org/2003/05/soap-envelope"); 
      Namespace soap1 = Namespace.getNamespace("soap1","http://schemas.microsoft.com/sharepoint/soap/"); 

      // drill down into elements 
      Element rootNode = doc.getRootElement(); 

      // Get Body node 
      Element body = rootNode.getChild("Body",soap); 
      // Get UpdateListItem Element 
      Element UpdateListItems = body.getChild("UpdateListItems",soap1); 
      // Get updates node 
      Element updates = UpdateListItems.getChild("updates",soap1); 

      // Set list name as String variable 
      String listNameString = UpdateListItems.getChild("listName",soap1).getText(); 

      // Print list text value ** THIS WORKS** 
      System.out.println(listNameString); 

不過,我似乎無法弄清楚如何選擇Field元素。 例如:我將如何選擇「標題」字段?

<Field Name="Title">New Item</Field> 

UPDATE:

我也能夠從「現場」元素獲取屬性「名稱」,而只能返回或設置的屬性值的名稱。我需要能夠訪問「Field」元素中的測試。

我能得到這樣的屬性值: System.out.println(field.getAttribute("Name").getValue()); // Prints Title

,我可以得到的名字是這樣的: System.out.println(field.getAttribute("Name").getName()); // Prints Name

不過,我需要能夠返回的文本值元件。

更新2: 我沒有提到。 XML看起來是這樣的:

` <?xml version="1.0" encoding="UTF-8"?> 
<soap:Envelope xmlns:soap="http://www.w3.org/2003/05/soap-envelope" xmlns:soap1="http://schemas.microsoft.com/sharepoint/soap/"> 
    <soap:Header/> 
    <soap:Body> 
     <soap1:UpdateListItems> 
     <soap1:listName>69A3FFFA-782B-45D5-B776-2BE6D5645745</soap1:listName> 
     <soap1:updates> 
      <Batch OnError="Continue"> 
      <Method ID="1" Cmd="New"> 
       <Field Name="Title">New Item</Field> 
       <Field Name="Classification" Type="Choice">Funny</Field> 
       <Field Name="Title">New Item</Field> 
       <Field Name="Title" Type="Text">Funny List Item</Field> 
      </Method> 
      </Batch> 
     </soap1:updates> 
     </soap1:UpdateListItems> 
    </soap:Body> 
</soap:Envelope>` 

我可以通過SoapUI提交這個到SharePoint,它的工作原理。但是如果有多個具有不同屬性的「字段」元素,我如何通過Jdom2選擇正確的元素?

我可以這樣做: String title = field.getText(); //returns New Item

但我將如何能夠抓住從使用「名稱」屬性其他「場」的元素的文本?

+0

括號?什麼布爾克? – rolfl 2014-09-26 21:06:56

+0

我需要能夠返回元素的文本值。 (New Item) '新項目' – luskbo 2014-09-27 00:36:24

回答

0

感謝您的幫助rolfl。我想到了。您可以遍歷子元素來訪問不同的「字段」屬性。然後我測試屬性名稱以獲取或設置其內容。這是我能想到的最好的。

for (Element node : method.getChildren("Field")){ 
     if(node.getAttributeValue("Name").equalsIgnoreCase("Title")){ 
      node.setText("String"); 
     } 
     System.out.println(node.getAttribute("Name").getValue()); 
    } 
1

它全部在命名空間中。你有三個,soap,soap1,還有默認的命名空間,在這種情況下,它是「」。 JDOM將這個名稱空間指定爲Namespace.NO_NAMESPACE。

所以,從updates元素得到字段元素,你可以這樣做:通過使用不具有在命名空間參數getChild方法

Element methods = updates.getChild("Method", Namespace.NO_NAMESPACE); 
Element field = methods.getChild("Field", Namespace.NO_NAMESPACE); 

這些可以更簡單,如果你願意,所有,如:

Element methods = updates.getChild("Method"); 
Element field = methods.getChild("Field"); 

看到這裏最重要的是,你的文件有3個命名空間,並且該Field元素(和方法太)不在肥皂,或soap1命名空間。

+0

正確,但我試圖在字段元素中爲標題的名稱屬性獲取值。在閱讀你的答案之前,我能夠複製你的答案。 Element field = method.getChild(「Field」);但是,我需要能夠訪問Title屬性的Field元素。到目前爲止,我可以通過以下屬性來獲得屬性「Title」:屬性title = field.getAttribute(「Title」);但似乎無法獲得其文本價值。 – luskbo 2014-09-26 19:44:54

+0

我想我是密集的....我可以;噸找出你是否想要的屬性值,或文本值。對於任何一個,如果你有'field',你可以得到屬性或值......但是,這沒有任何意義:*「...試圖獲取字段的名稱屬性的字段元素中的值「* ...它不加起來。 – rolfl 2014-09-26 20:40:44

+0

我剛更新了我的問題。我需要獲取括號內的值。它可能讓我變得更加困難。從此: '新條目' 我需要訪問文本「New Item」 – luskbo 2014-09-26 20:44:36