建议使用stax(xml流式api)
参考文献:
http://www.vogella.de/articles/JavaXML/article.html
示例XML
<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<SOAP-ENV:Body>
<ns:PicklistWS_GetPicklistValues_Output xmlns:ns="urn:crmondemand/ws/picklist/">
<ListOfParentPicklistValue xmlns="urn:/crmondemand/xml/picklist">
<ParentPicklistValue>
<Language>ENU</Language>
<ParentFieldName/>
<ParentDisplayValue/>
<ParentCode/>
<Disabled/>
<ListOfPicklistValue>
<PicklistValue>
<Code>F</Code>
<DisplayValue>F</DisplayValue>
<Disabled>N</Disabled>
</PicklistValue>
<PicklistValue>
<Code>M</Code>
<DisplayValue>M</DisplayValue>
<Disabled>N</Disabled>
</PicklistValue>
</ListOfPicklistValue>
</ParentPicklistValue>
</ListOfParentPicklistValue>
</ns:PicklistWS_GetPicklistValues_Output>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
正在分析代码:
static Map<String, String> getPicklistFromSoapResponse(String soapResponse) throws ServiceException {
Map<String, String> values = new LinkedHashMap<String, String>();
XMLInputFactory inputFactory = XMLInputFactory.newInstance();
String code = null;
String display = null;
String disabled = null;
try {
InputStream in = new ByteArrayInputStream(soapResponse.getBytes("UTF-8"));
XMLEventReader eventReader = inputFactory.createXMLEventReader(in);
while (eventReader.hasNext()) {
XMLEvent event = eventReader.nextEvent();
if (event.isStartElement()) {
if (event.asStartElement().getName().getLocalPart().equals("Code")) {
event = eventReader.nextEvent();
code = event.asCharacters().getData();
continue;
}
if (event.asStartElement().getName().getLocalPart().equals("DisplayValue")) {
event = eventReader.nextEvent();
display = event.asCharacters().getData();
continue;
}
if (event.asStartElement().getName().getLocalPart().equals("Disabled")) {
event = eventReader.nextEvent();
disabled = event.asCharacters().getData();
if ( "Y".equals(disabled)) values.put(code, display);
continue;
}
}
}
} catch (UnsupportedEncodingException e) {
throw new ServiceException(e);
} catch (XMLStreamException e) {
throw new ServiceException(e);
}
return values;
}
线:
InputStream in = new ByteArrayInputStream(soapResponse.getBytes("UTF-8"));
可以将文件切换为XML的源,其中:
InputStream in = new FileInputStream("myFile.xml");
在循环事件时,我们需要做的第一件事是使用
eventReader.nextEvent()
. 通常情况下,我们只关心作为开始元素的事件,它是用以下行检索的:
event.isStartElement()
检查一下我们看到的是不是一个打开的标签。例如:
<name>Fenton</name>
只有
<name>
部分是开始元素。因此,获取XML片段,调用
event.asStartElement().getName().getLocalPart()
结果为字符串:name。为了得到我们叫芬顿的绳子:
event.asCharacters().getData()
现在,有时元素将具有如下属性:
<name type="User">Fenton</name>
. 在这种情况下
type="User"
是一个属性,可以使用以下命令提取它:
StartElement startElement = event.asStartElement();
Iterator<Attribute> attributes = startElement.getAttributes();
while (attributes.hasNext()) {
Attribute attribute = attributes.next();
if (attribute.getName().toString().equals("type"));
String typeIsUser = attribute.getValue();
}