Java Web 服务,第 2 部分: 深度探索 Axis2:AXIOM
2009-11-05 00:00:00 来源:WEB开发网
清单 4. 导航 AXIOM /**
* Walk subtree for element. This recursively walks through the document
* nodes under an element, accumulating summary information.
*
* @param element element to be walked
* @param summary document summary information
*/
protected void walkElement(OMElement element, DocumentSummary summary) {
// include attribute values in summary
for (Iterator iter = element.getAllAttributes(); iter.hasNext();) {
OMAttribute attr = (OMAttribute)iter.next();
summary.addAttribute(attr.getAttributeValue().length());
}
// loop through children
for (Iterator iter = element.getChildren(); iter.hasNext();) {
// handle child by type
OMNode child = (OMNode)iter.next();
int type = child.getType();
if (type == OMNode.TEXT_NODE) {
summary.addContent(((OMText)child).getText().length());
} else if (type == OMNode.ELEMENT_NODE) {
summary.addElements(1);
walkElement((OMElement)child, summary);
}
}
}
/**
* Walk and summarize document. This method walks through the nodes
* of the document, accumulating summary information.
*
* @param doc document representation to be walked
* @param summary output document summary information
*/
protected void walk(Object doc, DocumentSummary summary) {
summary.addElements(1);
walkElement(((OMDocument)doc).getOMDocumentElement(), summary);
}
更多精彩
赞助商链接