1

Having some trouble returning certain fields from a SharePoint List SOAP request.

Here is the 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>

I am able to use the following Jdom2 code to grab certain values like this:

            // 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);

However, I can't seem to figure out how to select the Field elements. For example: How would I select the "Title" Field?

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

UPDATE:

I also able to get the attribute "Name" from the "Field" element, but can only return or set the name of value of the attribute. I need to be able to access the test within the "Field" Element.

I can get the value of the attribute like this: System.out.println(field.getAttribute("Name").getValue()); // Prints Title

And I can get the name like this: System.out.println(field.getAttribute("Name").getName()); // Prints Name

But, I need to be able to return the text value of the element.

UPDATE 2: I didn't mention. The XML really looks like this:

`    <?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>`

I can submit this via SoapUI to SharePoint and it works. But if there are multiple "Field" elements, with different attributes, how can I select the correct one via Jdom2?

I can do this: String title = field.getText(); //returns New Item

But how would I be able to grab the text from other "Field" elements that use the "Name" attribute?

4

2 回答 2

1

这一切都在命名空间中。您有其中三个,soap, soap1,还有默认命名空间,在本例中为“”。JDOM 将此命名空间指定为 Namespace.NO_NAMESPACE。

因此,要从元素中获取字段元素updates,您可以执行以下操作:

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

如果需要,可以使用根本没有命名空间参数的 getChild 方法使这些变得更简单,例如:

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

这里要看到的重要一点是,您的文档有 3 个命名空间,并且 Field 元素(以及 Method 也是)不在 soap 或 soap1 命名空间中。

于 2014-09-26T18:33:01.897 回答
0

感谢您的帮助。我想到了。您可以遍历子元素以访问不同的“字段”属性。然后我测试属性名称以获取或设置其内容。这是我能想到的最好的。

    for (Element node : method.getChildren("Field")){ 
        if(node.getAttributeValue("Name").equalsIgnoreCase("Title")){
            node.setText("String");
        }
        System.out.println(node.getAttribute("Name").getValue());
    }
于 2014-09-27T05:40:14.553 回答