/**
* delete single node
*
* @param xmlDoc
* @param xpathList
* @throws JDOMException
*/
protected void removeSingleNode(Document xmlDoc, List xpathList) throws JDOMException {
for (int i = 0; i < xpathList.size(); i++) {
String xpath = (String) xpathList.get(i);
Element el = selectSingleNode(xmlDoc, xpath);
if (el != null) {
el.getParentElement().removeContent(el);
}
}
}
/**
* delete multi nodes
*
* @param xmlDoc
* @param xpathList
* @throws JDOMException
*/
protected void removeNodes(Document xmlDoc, List xpathList) throws JDOMException {
for (int i = 0; i < xpathList.size(); i++) {
String xpath = (String) xpathList.get(i);
List list = selectNodes(xmlDoc, xpath);
for (int j = 0; j < list.size(); j++) {
Element el = (Element) list.get(j);
if (el != null) {
el.getParentElement().removeContent(el);
}
}
}
}
/**
* delete single node attribute
*
* @param xmlDoc
* @param xpathMap
* @throws JDOMException
*/
protected void removeSingleNodeAttribute(Document xmlDoc, Map xpathMap)
throws JDOMException {
Iterator xpathList = xpathMap.keySet().iterator();
while(xpathList.hasNext()){
String xpath = (String) xpathList.next();
Element el = selectSingleNode(xmlDoc, xpath);
if (el != null) {
String[] attributes = (String[])xpathMap.get(xpath);
for (int k = 0; k < attributes.length; k++) {
el.removeAttribute(attributes[k]);
}
}
}
}
/**
* delete multi nodes attribute
*
* @param xmlDoc
* @param xpathMap
* @throws JDOMException
*/
protected void removeNodesAttribute(Document xmlDoc, Map xpathMap)
throws JDOMException {
Iterator xpathList = xpathMap.keySet().iterator();
while(xpathList.hasNext()){
String xpath = (String) xpathList.next();
List list = selectNodes(xmlDoc, xpath);
for (int j = 0; j < list.size(); j++) {
Element el = (Element) list.get(j);
String[] attributes = (String[])xpathMap.get(xpath);
for (int k = 0; k < attributes.length; k++) {
el.removeAttribute(attributes[k]);
}
}
}
}
/**
* @param xmlDoc
* @param strXPath
* @return
* @throws JDOMException
*/
protected Element selectSingleNode(Document xmlDoc, String strXPath) throws JDOMException {
Element elRet = null;
XPath xpath = XPath.newInstance(strXPath);
xpath.addNamespace("ewcm", "http://w3.ibm.com/standards/xml/released/ewcm");
elRet = (Element) xpath.selectSingleNode(xmlDoc);
return elRet;
}
/**
* @param xmlDoc
* @param strXPath
* @return
* @throws JDOMException
*/
protected List selectNodes(Document xmlDoc, String strXPath) throws JDOMException {
XPath xpath = XPath.newInstance(strXPath);
xpath.addNamespace("ewcm", "http://w3.ibm.com/standards/xml/released/ewcm");
return xpath.selectNodes(xmlDoc);
}
==================================================================
// 由XML 字符串 生成org.jdom.Document 对象。
public static Document createDocument(String xml) throws XMLException {
try {
return new SAXBuilder().build(new ByteArrayInputStream(xml.getBytes("UTF-8")));
} catch (JDOMException e) {
throw new XMLException(e);
} catch (IOException e) {
throw new XMLException(e);
}
}
//返回org.jdom.Document 对象的字符串。
public static String toString(Document doc) {
XMLOutputter outputter = new XMLOutputter();
return outputter.outputString(doc);
}
//返回 org.jdom.Element 对象的字符串
public static String toString(Element el) {
XMLOutputter outputter = new XMLOutputter();
return outputter.outputString(el);
}
==============================================================
//取得XML中的Type节点的值
Document xmlDoc = XMLUtil.createDocument(xmlStr);
String typeXPath = "/ewcm:IBMWebContent/Metadata/Type";
Element elType = selectSingleNode(xmlDoc,typeXPath);
String type = elType.getText();