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 XPath |
createXPath()
创建XPath
Xpath相关文章:https://www.ibm.com/developerworks/cn/xml/x-javaxpathapi.html |
static String |
elementText(Element element,
String tagName)
根据节点名获得第一个子节点
|
static String |
elementText(Element element,
String tagName,
String defaultValue)
根据节点名获得第一个子节点
|
static Object |
getByXPath(String expression,
Object source,
QName returnType)
通过XPath方式读取XML节点等信息
Xpath相关文章:https://www.ibm.com/developerworks/cn/xml/x-javaxpathapi.html |
static Element |
getElement(Element element,
String tagName)
根据节点名获得第一个子节点
|
static List<Element> |
getElements(Element element,
String tagName)
根据节点名获得子节点列表
|
static Element |
getRootElement(Document doc)
获得XML文档根节点
|
static Document |
parseXml(String xmlStr)
将String类型的XML转换为XML文档
|
static <T> T |
readObjectFromXml(File source)
从XML中读取对象
Reads serialized object from the XML file.
|
static <T> T |
readObjectFromXml(InputSource source)
从XML中读取对象
Reads serialized object from the XML file.
|
static <T> T |
readObjectFromXml(String xmlStr)
从XML中读取对象
Reads serialized object from the XML file.
|
static Document |
readXML(File file)
读取解析XML文件
|
static Document |
readXML(InputSource source)
读取解析XML文件
编码在XML中定义 |
static Document |
readXML(InputStream inputStream)
读取解析XML文件
编码在XML中定义 |
static Document |
readXML(Reader reader)
读取解析XML文件
|
static Document |
readXML(String pathOrContent)
读取解析XML文件
如果给定内容以“<”开头,表示这是一个XML内容,直接读取,否则按照路径处理 路径可以为相对路径,也可以是绝对路径,相对路径相对于ClassPath |
static void |
toFile(Document doc,
String absolutePath)
将XML文档写入到文件
使用Document中的编码 |
static void |
toFile(Document doc,
String path,
String charset)
将XML文档写入到文件
|
static String |
toStr(Document doc)
将XML文档转换为String
字符编码使用XML文档中的编码,获取不到则使用UTF-8 |
static String |
toStr(Document doc,
String charset)
将XML文档转换为String
此方法会修改Document中的字符集 |
static String |
toStr(Document doc,
String charset,
boolean isPretty)
将XML文档转换为String
此方法会修改Document中的字符集 |
static List<Element> |
transElements(Element parentEle,
NodeList nodeList)
将NodeList转换为Element列表
非Element节点将被忽略 |
static List<Element> |
transElements(NodeList nodeList)
将NodeList转换为Element列表
|
static void |
write(Document doc,
Writer writer,
boolean isPretty)
将XML文档写出
|
static <T> void |
writeObjectAsXml(File dest,
T t)
将可序列化的对象转换为XML写入文件,已经存在的文件将被覆盖
Writes serializable object to a XML file. |
public static Document readXML(String pathOrContent)
pathOrContent - 内容或路径public static Document readXML(InputStream inputStream) throws UtilException
inputStream - XML流UtilException - IO异常或转换异常public static Document readXML(Reader reader) throws UtilException
reader - XML流UtilException - IO异常或转换异常public static Document readXML(InputSource source)
source - InputSourcepublic static Document parseXml(String xmlStr)
xmlStr - XML字符串public static <T> T readObjectFromXml(File source) throws IOException
T - 对象类型source - XML文件IOException - IO异常public static <T> T readObjectFromXml(String xmlStr) throws IOException
T - 对象类型xmlStr - XML内容IOException - IO异常public static <T> T readObjectFromXml(InputSource source) throws IOException
T - 对象类型source - InputSourceIOException - IO异常public static String toStr(Document doc)
doc - XML文档public static String toStr(Document doc, String charset)
doc - XML文档charset - 自定义XML文件的编码,如果为null 读取XML文档中的编码,否则默认UTF-8public static String toStr(Document doc, String charset, boolean isPretty)
doc - XML文档charset - 自定义XML文件的编码,如果为null 读取XML文档中的编码,否则默认UTF-8isPretty - 是否格式化输出public static void toFile(Document doc, String absolutePath)
doc - XML文档absolutePath - 文件绝对路径,不存在会自动创建public static void toFile(Document doc, String path, String charset)
doc - XML文档path - 文件路径绝对路径或相对ClassPath路径,不存在会自动创建charset - 自定义XML文件的编码,如果为null 读取XML文档中的编码,否则默认UTF-8public static void write(Document doc, Writer writer, boolean isPretty)
doc - Document XML文档writer - 写出的Writer,Writer决定了输出XML的编码isPretty - 是否格式化输出public static Document createXml(String rootElementName)
rootElementName - 根节点名称public static Element getRootElement(Document doc)
doc - DocumentDocument.getDocumentElement()public static String cleanInvalid(String xmlContent)
xmlContent - XML文本public static List<Element> getElements(Element element, String tagName)
element - 节点tagName - 节点名,如果节点名为空(null或blank),返回所有子节点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 - 节点名defaultValue - 默认值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 - 对象IOException - IO异常public static XPath createXPath()
XPathpublic static Object getByXPath(String expression, Object source, QName returnType)
expression - XPath表达式source - 资源,可以是Docunent、Node节点等returnType - 返回类型,XPathConstantsCopyright © 2018. All rights reserved.