我正在尝试从以下来自Salesforce的String响应中获取标签值,
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="http://soap.sforce.com/2006/04/metadata">
<soapenv:Body>
<listMetadataResponse>
<result>
<createdById>00528000001m5RRAAY</createdById>
<createdByName>Hariprasath Thanarajah</createdByName>
<createdDate>1970-01-01T00:00:00.000Z</createdDate>
<fileName>objects/EmailMessage.object</fileName>
<fullName>EmailMessage</fullName>
<id />
<lastModifiedById>00528000001m5RRAAY</lastModifiedById>
<lastModifiedByName>Hariprasath Thanarajah</lastModifiedByName>
<lastModifiedDate>1970-01-01T00:00:00.000Z</lastModifiedDate>
<namespacePrefix />
<type>CustomObject</type>
</result>
</listMetadataResponse>
</soapenv:Body>
</soapenv:Envelope>
Run Code Online (Sandbox Code Playgroud)
上面有标签<fullName>。我需要在标签内获取值并将其放入String数组。我已经尝试过使用substring方法,但是它仅返回一个值。有人可以建议我这样做吗?
我已经尝试过如下
public static Document loadXMLString(String response) throws Exception
{
DocumentBuilderFactory dbf =DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(response));
return db.parse(is);
}
public static List<String> getFullNameFromXml(String response, String tagName) throws Exception {
Document xmlDoc = loadXMLString(response);
NodeList nodeList = xmlDoc.getElementsByTagName(tagName);
List<String> ids = new ArrayList<String>(nodeList.getLength());
for(int i=0;i<nodeList.getLength(); i++) {
Node x = nodeList.item(i);
ids.add(x.getFirstChild().getNodeValue());
System.out.println(nodeList.item(i).getFirstChild().getNodeValue());
}
return ids;
}
Run Code Online (Sandbox Code Playgroud)
从上面的代码中,您将获得ID列表。之后,您可以将其放入String数组,然后将其返回String数组,如下所示,
List<String> output = getFullNameFromXml(response, "fullName");
String[] strarray = new String[output.size()];
output.toArray(strarray);
System.out.print("Response Array is "+Arrays.toString(strarray));
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
24196 次 |
| 最近记录: |