【问题标题】:Jdom2 Sharepoint XML FieldsJdom2 Sharepoint XML 字段
【发布时间】: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

但是我怎样才能从其他使用“名称”属性的“字段”元素中获取文本呢?

【问题讨论】:

  • 括号?什么括号?
  • 我需要能够返回元素的文本值。 (新项目)&lt;Field Name="Title"&gt;New Item&lt;/Field&gt;

标签: java xml sharepoint soap jdom-2


【解决方案1】:

这一切都在命名空间中。您有其中三个,soapsoap1,还有默认命名空间,在本例中为“”。 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 命名空间中。

【讨论】:

  • 正确,但我试图在字段元素中获取标题名称属性的值。在阅读您的答案之前,我能够复制您的答案。元素字段 = method.getChild("Field");但是,我需要能够访问 Title 属性的 Field 元素。到目前为止,我可以使用以下方法获取属性“Title”: Attribute title = field.getAttribute("Title");但似乎无法获取其文本值。
  • 我想我很密集....我无法弄清楚您想要的是属性值还是文本值。对于任何一个,如果你有field,你可以得到属性或值......但是,这没有任何意义:“......试图在字段元素中获取名称属性的值标题。”...它不加起来。
  • 我刚刚更新了我的问题。我需要得到括号内的值。我可能让这变得比它必须的更困难。从这里:&lt;Field Name="Title"&gt;New Item&lt;/Field&gt;我需要访问文本“新项目”
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 2016-07-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多