gpt4 book ai didi

java - Jdom2 共享点 XML 字段

转载 作者:太空宇宙 更新时间:2023-11-04 06:31:28 24 4
gpt4 key购买 nike

从 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>

更新:

我还可以从“Field”元素获取属性“Name”,但只能返回或设置属性值的名称。我需要能够访问“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

但是我如何才能从使用“Name”属性的其他“Field”元素中获取文本?

最佳答案

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

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

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 命名空间中。

关于java - Jdom2 共享点 XML 字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26064658/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com