Java Jdom2 Sharepoint XML字段
从SharePoint列表SOAP请求返回某些字段时遇到问题 以下是XML:Java Jdom2 Sharepoint XML字段,java,xml,sharepoint,soap,jdom-2,Java,Xml,Sharepoint,Soap,Jdom 2,从SharePoint列表SOAP请求返回某些字段时遇到问题 以下是XML: 69A3FFFA-782B-45D5-B776-2BE6D5645745 新项目 我能够使用以下Jdom2代码获取如下某些值: // set your name spaces. Namespace soap = Namespace.getNamespace("soap","http://www.w3.org/2003/05/soap-envelope");
69A3FFFA-782B-45D5-B776-2BE6D5645745
新项目
我能够使用以下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);
` <?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()//返回新项目
但是我如何才能从使用“Name”属性的其他“Field”元素中获取文本呢?它们都在名称空间中。您有三个名称空间,
soap
,soap1
,还有一个默认名称空间,在本例中是“”。JDOM将此名称空间指定为namespace.NO_名称空间
因此,要从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个名称空间,字段元素(以及方法)不在soap或soap1名称空间中。感谢您的帮助。我想出来了。可以通过子元素循环访问不同的“字段”属性。然后,我测试属性名以获取或设置其内容。这是我能想到的最好的了
for (Element node : method.getChildren("Field")){
if(node.getAttributeValue("Name").equalsIgnoreCase("Title")){
node.setText("String");
}
System.out.println(node.getAttribute("Name").getValue());
}
正确,但我正在尝试获取标题的Name属性的Field元素中的值。就在阅读你们的答案之前,我能够复制你们的答案。元素字段=method.getChild(“字段”);但是,我需要能够访问Title属性的Field元素。到目前为止,我可以通过以下方式获取属性“Title”:attribute Title=field.getAttribute(“Title”);但似乎无法得到它的文本值。我想我正在变得稠密。。。。我可以;t弄清楚您想要的是属性值还是文本值。如果您有
字段
,则可以获取属性或值。。。但是,这毫无意义:“…尝试获取标题的Name属性的Field元素中的值。”。。。我只是更新了我的问题。我需要得到括号内的值。有可能是我让这件事变得更加困难了。从此处:newitem
我需要访问文本“newitem”括号?什么brckets?我需要能够返回元素的文本值。(新项目)新项目
for (Element node : method.getChildren("Field")){
if(node.getAttributeValue("Name").equalsIgnoreCase("Title")){
node.setText("String");
}
System.out.println(node.getAttribute("Name").getValue());
}