java XML处理

/**
  * 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();

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值