dom4jhelper.java

来自「一个java工作流引擎」· Java 代码 · 共 40 行

JAVA
40
字号
package org.jbpm.util.xml;

import java.io.*;
import org.apache.commons.logging.*;
import org.dom4j.*;
import org.dom4j.io.*;

public class Dom4jHelper {

  public static Element getRootElement( String xml ) throws DocumentException {
    return getRootElement( xml.getBytes() );
  }
  
  public static Element getRootElement( byte[] bytes ) throws DocumentException {
    SAXReader saxReader = new SAXReader();
    Document document = saxReader.read( new ByteArrayInputStream( bytes ) );
    return document.getRootElement();
  }
  
  public static String getElementText( Element xmlElement, String name, String defaultValue ) {
    String text = defaultValue;
    Element subElement = xmlElement.element( name );
    if ( subElement != null ) {
      text = subElement.getTextTrim(); 
    }
    return text;
  }

  public static String getAttribute( Element xmlElement, String name, String defaultValue ) {
    String text = defaultValue;
    Attribute attribute = xmlElement.attribute( name );
    if ( attribute != null ) {
      text = attribute.getText(); 
    }
    return text;
  }

  private static Log log = LogFactory.getLog(Dom4jHelper.class);
}

⌨️ 快捷键说明

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