⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 operxml.java

📁 操作XML的源代码
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
      else
      {
        throw new Exception("节点" + iElem.getName() + "下没有此子节点:" + iKeyCode);
      }
    }
    catch(Exception e)
    {
      throw e;
    }
    return strValue;
  }

  /**
   * 获取(节点iElem下)标签为iKeyCode的第index+1个节点的值
   * @param iKeyCode 节点标签
   * @param iElem 节点Element
   * @param index 节点的下标
   * @return String 第index+1个节点的值
   * @throws JDOMException
   * @throws Exception
   */
  public String getKeyValue(Element iElem,String iKeyCode,int index) throws JDOMException,Exception
  {
    String strValue = null;
    List lKeyList = null;
    try
    {
      lKeyList = iElem.getChildren(iKeyCode);
      if(lKeyList.size()>0)
      {
        if(index>=lKeyList.size())
          throw new Exception("下标越界:" + index);
        else
        {
          Element item = (Element)lKeyList.get(index);
          strValue = item.getText();
        }
      }
      else
      {
        throw new Exception("根下没有此节点:" + iKeyCode);
      }
    }
    catch(Exception e)
    {
      throw e;
    }
    return strValue;
  }


  /**
   * 设置节点iElem属性为iAttributeCode的值
   * @param iAttributeCode 节点的值
   * @param iElem 节点Element
   * @throws JDOMException
   * @throws Exception
   */
  public String getAttribute(Element iElem,String iAttributeCode) throws JDOMException,Exception
  {
    Attribute attr = null;
    String strAttributeValue = "";
    try
    {
       attr = iElem.getAttribute(iAttributeCode);
       if(attr==null)
       {
          throw new Exception("");
       }
       else
         strAttributeValue = attr.getValue();
    }
    catch(Exception e)
    {
      throw e;
    }
    return strAttributeValue;
  }

  /**
   * 获取(节点iElem下)标签为iKeyCode的第一个节点
   * @param iKeyCode 节点标签
   * @param iElem 节点Element
   * @return Element 第一个节点
   * @throws JDOMException
   * @throws Exception
   */
  public Element getElement(Element iElem,String iKeyCode) throws JDOMException,Exception
  {
    Element element = null;
    List lKeyList = null;
    try
    {
      lKeyList = iElem.getChildren(iKeyCode);
      if(lKeyList.size()>0)
      {
        element = (Element)lKeyList.get(0);
      }
      else
      {
        throw new Exception("节点" + iElem.getName() + "下没有此子节点:" + iKeyCode);
      }
    }
    catch(Exception e)
    {
      throw e;
    }
    return element;
  }

  /**
   * 获取(节点iElem下)标签为iKeyCode的第index+1个节点
   * @param iKeyCode 节点标签
   * @param iElem 节点Element
   * @param index 节点的下标
   * @return Element 第index+1个节点
   * @throws JDOMException
   * @throws Exception
   */
  public Element getElement(Element iElem,String iKeyCode,int index) throws JDOMException,Exception
  {
    Element element = null;
    List lKeyList = null;
    try
    {
      lKeyList = iElem.getChildren(iKeyCode);
      if(lKeyList.size()>0)
      {
        if(index>=lKeyList.size())
          throw new Exception("下标越界:" + index);
        else
          element = (Element)lKeyList.get(index);
      }
      else
      {
        throw new Exception("节点" + iElem.getName() + "下没有此子节点:" + iKeyCode);
      }
    }
    catch(Exception e)
    {
      throw e;
    }
    return element;
  }

  /**
   * 获取(根节点下)标签为iKeyCode的第一个节点
   * @param iKeyCode 节点标签
   * @return Element 第一个节点
   * @throws JDOMException
   * @throws Exception
   */
  public Element getElement(String iKeyCode) throws JDOMException,Exception
  {
    Element element = null;
    List lKeyList = null;
    try
    {
      lKeyList = root.getChildren(iKeyCode);
      if(lKeyList.size()>0)
      {
        element = (Element)lKeyList.get(0);
      }
      else
      {
        throw new Exception("根下没有此子节点:" + iKeyCode);
      }
    }
    catch(Exception e)
    {
      throw e;
    }
    return element;
  }

  /**
   * 获取(根节点下)标签为iKeyCode的第index+1个节点
   * @param iKeyCode 节点标签
   * @param index 节点的下标
   * @return Element 第index+1个节点
   * @throws JDOMException
   * @throws Exception
   */
  public Element getElement(String iKeyCode,int index) throws JDOMException,Exception
  {
    Element element = null;
    List lKeyList = null;
    try
    {
      lKeyList = root.getChildren(iKeyCode);
      if(lKeyList.size()>0)
      {
        if(index>=lKeyList.size())
          throw new Exception("下标越界:" + index);
        else
          element = (Element)lKeyList.get(index);
      }
      else
      {
        throw new Exception("根下没有此子节点:" + iKeyCode);
      }
    }
    catch(Exception e)
    {
      throw e;
    }
    return element;
  }

  /**
   * 设置节点iElem的值
   * @param iKeyValue 节点的值
   * @param iElem 节点Element
   * @throws JDOMException
   * @throws Exception
   */
  public void setKeyValue(Element iElem,String iKeyValue) throws JDOMException,Exception
  {
    List lKeyList = null;
    try
    {
      lKeyList = iElem.getChildren();
      if(lKeyList.size()>0)
      {
         throw new Exception("节点" + iElem.getName() + "下还有子节点,不能设置该节点的值!");
      }
      else
        iElem.setText(iKeyValue);
    }
    catch(Exception e)
    {
      throw e;
    }
  }

  /**
   * 设置节点iElem属性iAttributeCode的值iAttributeValue
   * @param iAttributeCode 节点的属性的名称
   * @param iAttributeValue 节点的值
   * @param iElem 节点Element
   * @param blFlag 标志,如果不在在该属性时true增加/false不增加并抛出异常
   * @throws JDOMException
   * @throws Exception
   */
  public void setAttribute(Element iElem,String iAttributeCode,String iAttributeValue,boolean blFlag) throws JDOMException,Exception
  {
    Attribute attr = null;
    try
    {
      attr = iElem.getAttribute(iAttributeCode);
      if(attr==null)
      {
        if(blFlag)
        {
          iElem.setAttribute(iAttributeCode,iAttributeValue);
        }
        else
          throw new Exception("节点" + iElem.getName() + "没有属性" + iAttributeCode + ",不能设置该属性的值!");
      }
      else
        iElem.setAttribute(iAttributeCode,iAttributeValue);
    }
    catch(Exception e)
    {
      throw e;
    }
  }

  /**
   * 设置节点iElem属性iAttributeCode的值iAttributeValue
   * @param iAttributeCode 节点的属性的名称
   * @param iAttributeValue 节点的值
   * @param iElem 节点Element
   * @throws JDOMException
   * @throws Exception
   */
  public void setAttribute(Element iElem,String iAttributeCode,String iAttributeValue) throws JDOMException,Exception
  {
    try
    {
      setAttribute(iElem,iAttributeCode,iAttributeValue,false);
    }
    catch(Exception e)
    {
      throw e;
    }
  }

  /**
   * 在节点iParentElem下增加一个子节点
   * @param iParentElem 父节点
   * @param iSubElem 子节点
   * @throws JDOMException
   * @throws Exception
   */
  public void addElement(Element iParentElem,Element iSubElem) throws JDOMException,Exception
  {
    try
    {
      iParentElem.addContent(iSubElem);
    }
    catch(Exception e)
    {
      throw e;
    }
  }

  /**
   * 在节点iParentElem下增加一个子节点
   * @param iParentElem 父节点
   * @param iSubElem 子节点
   * @param index 子节点添加的位置
   * @throws JDOMException
   * @throws Exception
   */
  public void addElement(Element iParentElem,Element iSubElem,int index) throws JDOMException,Exception
  {
    try
    {
      iParentElem.addContent(index,iSubElem);
    }
    catch(Exception e)
    {
      throw e;
    }
  }


  /**
   * 创建节点
   * @param iElemCode 节点的名称
   * @param iElemValue 节点的值
   * @return Element 要创建的节点
   * @throws JDOMException
   * @throws Exception
   */
  public Element createElement(String iElemCode,String iElemValue) throws JDOMException,Exception
  {
    Element elem = null;
    try
    {
      elem = new Element(iElemCode,iElemValue);
    }
    catch(Exception e)
    {
      throw e;
    }
    return elem;
  }

  /**
   * 创建文档
   * @param iRootName 根节点的名称
   */
  public void createDocument(String iRootName) throws JDOMException,Exception
  {
     try {
       if(doc==null)
         doc = new Document(new Element(iRootName));
         root = doc.getRootElement();
     }
     catch (Exception e) {
       e.printStackTrace();
     }
  }

  /**
   * 保存文档
   * @param outputFileSrc 要保存的文档的路径
   */
  public void saveDocument(String outputFileSrc) throws IOException,JDOMException,Exception
  {
      try {
          XMLOutputter outputter;
          outputter = new XMLOutputter("  ",false,"GB2312");
          outputter.output(doc, new FileOutputStream(outputFileSrc));
      }
      catch (IOException ioe) {
          ioe.printStackTrace();
      }
      catch (Exception e) {
          e.printStackTrace();
      }
  }

  /**
   * 主函数
   */
  public static void main(String[] args) {
     OperXML operXML = new OperXML();
     try
     {
        operXML.parserFromXMLString("<?xml version=\"1.0\" encoding=\"UTF-8\"?><First><Second><Third><![CDATA[<AAA>My Name Is Yang]]></Third></Second><Second><Third>My Name IsIS Wang</Third></Second></First>");
        Element eSecond = operXML.getElement("Second",0);
        System.out.println("Value : " + operXML.getKeyValue(eSecond,"Third"));
     }
     catch(Exception e)
     {
       e.printStackTrace();
     }
  }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -