public class XmlUtil extends Object
| 限定符和类型 | 字段和说明 |
|---|---|
static String |
INVALID_REGEX
在XML中无效的字符 正则
|
| 构造器和说明 |
|---|
XmlUtil() |
| 限定符和类型 | 方法和说明 |
|---|---|
static String |
cleanInvalid(String xmlContent)
去除XML文本中的无效字符
|
static Document |
createXml(String rootElementName)
创建XML文档
创建的XML默认是utf8编码,修改编码的过程是在toStr和toFile方法里,既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 <T> T |
readObjectFromXml(File source)
从XML中读取对象
Reads serialized object from the XML file.
|
static Document |
readXML(File file)
读取解析XML文件
|
static Document |
readXML(String absoluteFilePath)
读取解析XML文件
|
static void |
toFile(Document doc,
String absolutePath)
将XML文档写入到文件
使用Document中的编码 |
static void |
toFile(Document doc,
String absolutePath,
String charset)
将XML文档写入到文件
|
static String |
toStr(Document doc)
将XML文档转换为String
|
static String |
toStr(Document doc,
String charset)
将XML文档转换为String
此方法会修改Document中的字符集 |
static List<Element> |
transElements(Element parentEle,
NodeList nodeList)
将NodeList转换为Element列表
|
static List<Element> |
transElements(NodeList nodeList)
将NodeList转换为Element列表
|
static <T> void |
writeObjectAsXml(File dest,
T t)
将可序列化的对象转换为XML写入文件,已经存在的文件将被覆盖
Writes serializable object to a XML file. |
public static Document readXML(String absoluteFilePath)
absoluteFilePath - XML文件绝对路径public static Document parseXml(String xmlStr)
xmlStr - XML字符串public static String toStr(Document doc, String charset)
doc - XML文档charset - 自定义XML的字符集public static void toFile(Document doc, String absolutePath)
doc - XML文档absolutePath - 文件绝对路径,不存在会自动创建public static void toFile(Document doc, String absolutePath, String charset)
doc - XML文档absolutePath - 文件绝对路径,不存在会自动创建charset - 自定义XML文件的编码public static Document createXml(String rootElementName)
rootElementName - 根节点名称public static String cleanInvalid(String xmlContent)
xmlContent - XML文本public static List<Element> getElements(Element element, String tagName)
element - 节点tagName - 节点名public static Element getElement(Element element, String tagName)
element - 节点tagName - 节点名public static String elementText(Element element, String tagName)
element - 节点tagName - 节点名public static String elementText(Element element, String tagName, String defaultValue)
element - 节点tagName - 节点名public static List<Element> transElements(NodeList nodeList)
nodeList - NodeListpublic static List<Element> transElements(Element parentEle, NodeList nodeList)
parentEle - 父节点,如果指定将返回此节点的所有直接子节点,nul返回所有就节点nodeList - NodeListpublic static <T> void writeObjectAsXml(File dest, T t) throws IOException
T - dest - 目标文件t - 对象IOExceptionpublic static <T> T readObjectFromXml(File source) throws IOException
T - source - XML文件IOExceptionCopyright © 2016. All rights reserved.