【发布时间】:2014-09-26 17:00:48
【问题描述】:
从 SharePoint 列表 SOAP 请求返回某些字段时遇到一些问题。
这是 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>
更新:
我也可以从“字段”元素中获取属性“名称”,但只能返回或设置属性值的名称。我需要能够访问“字段”元素中的测试。
我可以像这样获取属性的值:
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,它可以正常工作。但是如果有多个“Field”元素,属性不同,如何通过Jdom2选择正确的?
我可以这样做:
String title = field.getText(); //returns New Item
但是我怎样才能从其他使用“名称”属性的“字段”元素中获取文本呢?
【问题讨论】:
-
括号?什么括号?
-
我需要能够返回元素的文本值。 (新项目)
<Field Name="Title">New Item</Field>
标签: java xml sharepoint soap jdom-2