private void handleBookListRequest(SOAPBody replyBody) throws SOAPException {
// Create a BookTitles element containing an entry
// for each book title.
SOAPBodyElement bodyElement = replyBody.addBodyElement(BOOK_TITLES_NAME);
// Add 'xsi:type = "SOAP-ENC:Array"'
bodyElement.addAttribute(
soapFactory.createName("type", XMLSCHEMA_INSTANCE_PREFIX,
XMLSCHEMA_INSTANCE_URI), SOAP_ENC_PREFIX + ":Array");
// Add 'SOAP-ENC:arrayType = "xsd:string[]"
bodyElement.addAttribute(
soapFactory.createName("arrayType", SOAP_ENC_PREFIX,
SOAPConstants.URI_NS_SOAP_ENCODING), XMLSCHEMA_PREFIX +
":string[]");
// Add an array entry for each book
String[] titles = BookImageServletData.getBookTitles( );
for (int i = 0; i < titles.length; i++) {
SOAPElement titleElement =
bodyElement.addChildElement("item");
titleElement.addTextNode(titles[i]);
}
}
注意红色字体,addChildElement
返回:
一个实例,表示实际添加到树的新 SOAP 元素。
以后要使用bodyElement才能添加他的子元素,从而形成树。