com.xiaoleilu.hutool
类 XmlUtil

java.lang.Object
  继承者 com.xiaoleilu.hutool.XmlUtil

public class XmlUtil
extends Object

XML工具类
此工具使用w3c dom工具,不需要依赖第三方包。

作者:
xiaoleilu

字段摘要
static String INVALID_REGEX
          在XML中无效的字符 正则
 
构造方法摘要
XmlUtil()
           
 
方法摘要
static String cleanInvalid(String xmlContent)
          去除XML文本中的无效字符
static Document createXml(String rootElementName)
          创建XML文档
static String elementText(Element element, String tagName)
          根据节点名获得第一个子节点
static String elementText(Element element, String tagName, String defaultValue)
          根据节点名获得第一个子节点
static Element getElement(Element element, String tagName)
          根据节点名获得第一个子节点
static List<Element> getElements(Element element, String tagName)
          根据节点名获得子节点列表
static Document parseXml(String xmlStr)
          将String类型的XML转换为XML文档
static Document parseXml(String xmlStr, String charset)
          将String类型的XML转换为XML文档
static Document readXML(File file)
          读取解析XML文件
static Document readXML(String absoluteFilePath)
          读取解析XML文件
static List<Element> transElements(Element parentEle, NodeList nodeList)
          将NodeList转换为Element列表
static List<Element> transElements(NodeList nodeList)
          将NodeList转换为Element列表
static void xmlToFile(Document doc, String absolutePath, String charset)
          将XML文档写入到文件
static String xmlToStr(Document doc)
          将XML文档转换为String
 
从类 java.lang.Object 继承的方法
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

字段详细信息

INVALID_REGEX

public static final String INVALID_REGEX
在XML中无效的字符 正则

另请参见:
常量字段值
构造方法详细信息

XmlUtil

public XmlUtil()
方法详细信息

readXML

public static Document readXML(File file)
读取解析XML文件

参数:
file - XML文件
返回:
XML文档对象

readXML

public static Document readXML(String absoluteFilePath)
读取解析XML文件

参数:
absoluteFilePath - XML文件绝对路径
返回:
XML文档对象

parseXml

public static Document parseXml(String xmlStr,
                                String charset)
将String类型的XML转换为XML文档

参数:
xmlStr - XML字符串
charset -
返回:
XML文档

parseXml

public static Document parseXml(String xmlStr)
将String类型的XML转换为XML文档

参数:
xmlStr - XML字符串
返回:
XML文档

xmlToStr

public static String xmlToStr(Document doc)
将XML文档转换为String

参数:
doc - XML文档
返回:
XML字符串

xmlToFile

public static void xmlToFile(Document doc,
                             String absolutePath,
                             String charset)
将XML文档写入到文件

参数:
doc - XML文档
absolutePath - 文件绝对路径,不存在会自动创建
charset - 字符集

createXml

public static Document createXml(String rootElementName)
创建XML文档

参数:
rootElementName - 根节点名称
返回:
XML文档

cleanInvalid

public static String cleanInvalid(String xmlContent)
去除XML文本中的无效字符

参数:
xmlContent - XML文本
返回:
当传入为null时返回null

getElements

public static List<Element> getElements(Element element,
                                        String tagName)
根据节点名获得子节点列表

参数:
element - 节点
tagName - 节点名
返回:
节点列表

getElement

public static Element getElement(Element element,
                                 String tagName)
根据节点名获得第一个子节点

参数:
element - 节点
tagName - 节点名
返回:
节点

elementText

public static String elementText(Element element,
                                 String tagName)
根据节点名获得第一个子节点

参数:
element - 节点
tagName - 节点名
返回:
节点中的值

elementText

public static String elementText(Element element,
                                 String tagName,
                                 String defaultValue)
根据节点名获得第一个子节点

参数:
element - 节点
tagName - 节点名
返回:
节点中的值

transElements

public static List<Element> transElements(NodeList nodeList)
将NodeList转换为Element列表

参数:
nodeList - NodeList
返回:
Element列表

transElements

public static List<Element> transElements(Element parentEle,
                                          NodeList nodeList)
将NodeList转换为Element列表

参数:
parentEle - 父节点,如果指定将返回此节点的所有直接子节点,nul返回所有就节点
nodeList - NodeList
返回:
Element列表


Copyright © 2014. All rights reserved.