以下来自salesforce describeSObject请求的响应为String,
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns="urn:partner.soap.sforce.com" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Header>
<LimitInfoHeader>
<limitInfo>
<current>73</current>
<limit>15000</limit>
<type>API REQUESTS</type>
</limitInfo>
</LimitInfoHeader>
</soapenv:Header>
<soapenv:Body>
<describeSObjectResponse>
<result>
<activateable>false</activateable>
<childRelationships>
<cascadeDelete>false</cascadeDelete>
<childSObject>Account</childSObject>
</childRelationships>
<compactLayoutable>true</compactLayoutable>
<createable>true</createable>
<custom>false</custom>
<customSetting>false</customSetting>
<deletable>true</deletable>
<deprecatedAndHidden>false</deprecatedAndHidden>
<feedEnabled>true</feedEnabled>
<fields>
<name>Id</name>
<nameField>false</nameField>
</fields>
<fields>
<name>IsDeleted</name>
<nameField>false</nameField>
</fields>
<name>Account</name>
<recordTypeInfos>
<name>Master</name>
</recordTypeInfos>
<supportedScopes>
<label>All accounts</label>
<name>everything</name>
</supportedScopes>
<supportedScopes>
<label>My accounts</label>
<name>mine</name>
</supportedScopes>
<supportedScopes>
<label>My team's accounts</label>
<name>team</name>
</supportedScopes>
</result>
</describeSObjectResponse>
</soapenv:Body>
从上面的响应中,我必须从describeSObjectResponse / result / fields / name标签中提取值并将其放入String数组中。当我尝试下面的时候,我从所有名称标签中获得了值。任何人都可以帮助我克服这一点。
public static Document loadXML(String xml) throws Exception
{
DocumentBuilderFactory fctr = DocumentBuilderFactory.newInstance();
DocumentBuilder bldr = fctr.newDocumentBuilder();
InputSource insrc = new InputSource(new StringReader(xml));
return bldr.parse(insrc);
}
public static List<String> getIdFromXml(String xml, String tagName) throws Exception {
Document xmlDoc = loadXML(xml);
NodeList nodeList = xmlDoc.getElementsByTagName("name");
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());
}
return ids;
}
答案 0 :(得分:0)
返回的此XML数据是否随每个请求而变化。我的意思是内容可以改变,但节点是否会改变? 如果没有,您可以为整个XML创建Bean类,然后使用JAXB来设置bean类。这样你就可以得到你想要的任何价值。
答案 1 :(得分:0)
最后找到了一种提取所需标签值的方法,如下所示,
public static Document loadXML(String xml) throws Exception
{
DocumentBuilderFactory fctr = DocumentBuilderFactory.newInstance();
DocumentBuilder bldr = fctr.newDocumentBuilder();
InputSource insrc = new InputSource(new StringReader(xml));
return bldr.parse(insrc);
}
public static List<String> getInnerTagFromXml(String xml, String tagName) throws Exception {
Document xmlDoc = loadXML(xml);
xmlDoc.getDocumentElement().normalize();
NodeList nodeList = xmlDoc.getElementsByTagName("fields");
List<String> ids = new ArrayList<String>(nodeList.getLength());
for (int temp = 0; temp < nodeList.getLength(); temp++) {
Node nNode = nodeList.item(temp);
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
ids.add(eElement.getElementsByTagName("name").item(0).getTextContent());
}
}
return ids;
}