public class Xml extends Object
| 限定符和类型 | 类和说明 |
|---|---|
static class |
Xml.UniversalNamespaceCache
全局命名空间上下文
见:https://www.ibm.com/developerworks/cn/xml/x-nmspccontext/ |
| 限定符和类型 | 字段和说明 |
|---|---|
static String |
AMP
字符串常量:XML And 符转义
"&" -> "&" |
static String |
APOS
字符串常量:XML 单引号转义
"&apos" -> "'" |
static String |
COMMENT_REGEX
在XML中注释的内容 正则
|
static String |
GT
字符串常量:XML 大于号转义
">" -> ">" |
static int |
INDENT_DEFAULT
XML格式化输出默认缩进量
|
static String |
INVALID_REGEX
在XML中无效的字符 正则
|
static String |
LT
字符串常量:XML 小于号转义
"<" -> "<" |
static String |
NBSP
字符串常量:XML 不间断空格转义
" " -> " " |
static String |
QUOTE
字符串常量:XML 双引号转义
""" -> "\"" |
| 构造器和说明 |
|---|
Xml() |
| 限定符和类型 | 方法和说明 |
|---|---|
static void |
append(Node node,
Object data)
追加数据子节点,可以是Map、集合、文本
|
static Element |
appendChild(Node node,
String tagName)
在已有节点上创建子节点
|
static Element |
appendChild(Node node,
String tagName,
String namespace)
在已有节点上创建子节点
|
static Node |
appendText(Node node,
CharSequence text)
创建文本子节点
|
static Document |
beanToXml(Object bean)
将Bean转换为XML
|
static Document |
beanToXml(Object bean,
String namespace)
将Bean转换为XML
|
static Document |
beanToXml(Object bean,
String namespace,
boolean ignoreNull)
将Bean转换为XML
|
static String |
cleanComment(String xmlContent)
去除XML文本中的注释内容
|
static String |
cleanInvalid(String xmlContent)
去除XML文本中的无效字符
|
static DocumentBuilder |
createDocumentBuilder()
创建 DocumentBuilder
|
static DocumentBuilderFactory |
createDocumentBuilderFactory()
创建
DocumentBuilderFactory
默认使用"com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl"如果使用第三方实现,请调用 disableDefaultDocumentBuilderFactory()
|
static Document |
createXml()
创建XML文档
创建的XML默认是utf8编码,修改编码的过程是在toStr和toFile方法里,即XML在转为文本的时候才定义编码 |
static Document |
createXml(String rootElementName)
创建XML文档
创建的XML默认是utf8编码,修改编码的过程是在toStr和toFile方法里,即XML在转为文本的时候才定义编码 |
static Document |
createXml(String rootElementName,
String namespace)
创建XML文档
创建的XML默认是utf8编码,修改编码的过程是在toStr和toFile方法里,即XML在转为文本的时候才定义编码 |
static XPath |
createXPath()
创建XPath
Xpath相关文章:https://www.ibm.com/developerworks/cn/xml/x-javaxpathapi.html |
static void |
disableDefaultDocumentBuilderFactory()
禁用默认的DocumentBuilderFactory,禁用后如果有第三方的实现(如oracle的xdb包中的xmlparse),将会自动加载实现。
|
static String |
elementText(Element element,
String tagName)
根据节点名获得第一个子节点
|
static String |
elementText(Element element,
String tagName,
String defaultValue)
根据节点名获得第一个子节点
|
static String |
escape(String string)
转义XML特殊字符:
& (ampersand) 替换为 &
< (小于) 替换为 <
> (大于) 替换为 >
" (双引号) 替换为 "
|
static String |
format(Document doc)
格式化XML输出
|
static String |
format(String xmlStr)
格式化XML输出
|
static Object |
getByXPath(String expression,
Object source,
QName returnType)
通过XPath方式读取XML节点等信息
Xpath相关文章:https://www.ibm.com/developerworks/cn/xml/x-javaxpathapi.html |
static Object |
getByXPath(String expression,
Object source,
QName returnType,
NamespaceContext nsContext)
通过XPath方式读取XML节点等信息
Xpath相关文章: https://www.ibm.com/developerworks/cn/xml/x-javaxpathapi.html https://www.ibm.com/developerworks/cn/xml/x-nmspccontext/ |
static Element |
getElement(Element element,
String tagName)
根据节点名获得第一个子节点
|
static Element |
getElementByXPath(String expression,
Object source)
通过XPath方式读取XML节点等信息
Xpath相关文章:https://www.ibm.com/developerworks/cn/xml/x-javaxpathapi.html |
static List<Element> |
getElements(Element element,
String tagName)
根据节点名获得子节点列表
|
static Node |
getNodeByXPath(String expression,
Object source)
通过XPath方式读取XML节点等信息
Xpath相关文章:https://www.ibm.com/developerworks/cn/xml/x-javaxpathapi.html |
static NodeList |
getNodeListByXPath(String expression,
Object source)
通过XPath方式读取XML的NodeList
Xpath相关文章:https://www.ibm.com/developerworks/cn/xml/x-javaxpathapi.html |
static Document |
getOwnerDocument(Node node)
获取节点所在的Document
|
static Element |
getRootElement(Document doc)
获得XML文档根节点
|
static boolean |
isElement(Node node)
给定节点是否为
Element 类型节点 |
static Document |
mapToXml(Map<?,?> data,
String rootName)
将Map转换为XML
|
static Document |
mapToXml(Map<?,?> data,
String rootName,
String namespace)
将Map转换为XML
|
static String |
mapToXmlStr(Map<?,?> data)
将Map转换为XML格式的字符串
|
static String |
mapToXmlStr(Map<?,?> data,
boolean omitXmlDeclaration)
将Map转换为XML格式的字符串
|
static String |
mapToXmlStr(Map<?,?> data,
String rootName)
将Map转换为XML格式的字符串
|
static String |
mapToXmlStr(Map<?,?> data,
String rootName,
String namespace)
将Map转换为XML格式的字符串
|
static String |
mapToXmlStr(Map<?,?> data,
String rootName,
String namespace,
boolean omitXmlDeclaration)
将Map转换为XML格式的字符串
|
static String |
mapToXmlStr(Map<?,?> data,
String rootName,
String namespace,
boolean isPretty,
boolean omitXmlDeclaration)
将Map转换为XML格式的字符串
|
static String |
mapToXmlStr(Map<?,?> data,
String rootName,
String namespace,
String charset,
boolean isPretty,
boolean omitXmlDeclaration)
将Map转换为XML格式的字符串
|
static Document |
parseXml(String xmlStr)
将String类型的XML转换为XML文档
|
static void |
readBySax(File file,
ContentHandler contentHandler)
使用Sax方式读取指定的XML
如果用户传入的contentHandler为 DefaultHandler,则其接口都会被处理 |
static void |
readBySax(InputSource source,
ContentHandler contentHandler)
使用Sax方式读取指定的XML
如果用户传入的contentHandler为 DefaultHandler,则其接口都会被处理 |
static void |
readBySax(InputStream source,
ContentHandler contentHandler)
使用Sax方式读取指定的XML
如果用户传入的contentHandler为 DefaultHandler,则其接口都会被处理 |
static void |
readBySax(Reader reader,
ContentHandler contentHandler)
使用Sax方式读取指定的XML
如果用户传入的contentHandler为 DefaultHandler,则其接口都会被处理 |
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 |
setNamespaceAware(boolean isNamespaceAware)
设置是否打开命名空间支持,默认打开
|
static void |
toFile(Document doc,
String absolutePath)
将XML文档写入到文件
使用Document中的编码 |
static void |
toFile(Document doc,
String path,
String charsetName)
将XML文档写入到文件
|
static String |
toStr(Document doc)
|
static String |
toStr(Document doc,
boolean isPretty)
将XML文档转换为String
字符编码使用XML文档中的编码,获取不到则使用UTF-8 |
static String |
toStr(Document doc,
String charset,
boolean isPretty)
将XML文档转换为String
字符编码使用XML文档中的编码,获取不到则使用UTF-8 |
static String |
toStr(Node doc)
|
static String |
toStr(Node doc,
boolean isPretty)
将XML文档转换为String
字符编码使用XML文档中的编码,获取不到则使用UTF-8 |
static String |
toStr(Node doc,
String charset,
boolean isPretty)
将XML文档转换为String
字符编码使用XML文档中的编码,获取不到则使用UTF-8 |
static String |
toStr(Node doc,
String charset,
boolean isPretty,
boolean omitXmlDeclaration)
将XML文档转换为String
字符编码使用XML文档中的编码,获取不到则使用UTF-8 |
static List<Element> |
transElements(Element parentEle,
NodeList nodeList)
将NodeList转换为Element列表
非Element节点将被忽略 |
static List<Element> |
transElements(NodeList nodeList)
将NodeList转换为Element列表
|
static void |
transform(Source source,
Result result,
String charset,
int indent)
将XML文档写出
格式化输出逻辑参考:https://stackoverflow.com/questions/139076/how-to-pretty-print-xml-from-java |
static void |
transform(Source source,
Result result,
String charset,
int indent,
boolean omitXmlDeclaration)
将XML文档写出
格式化输出逻辑参考:https://stackoverflow.com/questions/139076/how-to-pretty-print-xml-from-java |
static String |
unescape(String string)
反转义XML特殊字符:
|
static void |
write(Node node,
OutputStream out,
String charset,
int indent)
将XML文档写出
|
static void |
write(Node node,
OutputStream out,
String charset,
int indent,
boolean omitXmlDeclaration)
将XML文档写出
|
static void |
write(Node node,
Writer writer,
String charset,
int indent)
将XML文档写出
|
static void |
write(Node node,
Writer writer,
String charset,
int indent,
boolean omitXmlDeclaration)
将XML文档写出
|
static void |
writeObjectAsXml(File dest,
Object bean)
将可序列化的对象转换为XML写入文件,已经存在的文件将被覆盖
Writes serializable object to a XML file. |
static <T> T |
xmlToBean(Node node,
Class<T> bean)
XML转Java Bean
|
static Map<String,Object> |
xmlToMap(Node node)
XML格式字符串转换为Map
|
static Map<String,Object> |
xmlToMap(Node node,
Map<String,Object> result)
XML节点转换为Map
|
static Map<String,Object> |
xmlToMap(String xmlStr)
XML格式字符串转换为Map
|
static Map<String,Object> |
xmlToMap(String xmlStr,
Map<String,Object> result)
XML格式字符串转换为Map
只支持第一级别的XML,不支持多级XML |
public static final int INDENT_DEFAULT
public static void disableDefaultDocumentBuilderFactory()
public static void setNamespaceAware(boolean isNamespaceAware)
isNamespaceAware - 是否命名空间支持public static Document readXML(@Nonnull File file) throws IOException
file - XML文件IOExceptionpublic static Document readXML(@Nonnull String pathOrContent) throws IOException
pathOrContent - 内容或路径IOExceptionpublic static Document readXML(InputStream inputStream) throws IOException
inputStream - XML流IOExceptionpublic static Document readXML(Reader reader) throws IOException
reader - XML流IOExceptionpublic static Document readXML(InputSource source) throws IOException
source - InputSourceIOExceptionpublic static void readBySax(File file, ContentHandler contentHandler) throws IOException
DefaultHandler,则其接口都会被处理file - XML源文件,使用后自动关闭contentHandler - XML流处理器,用于按照Element处理xmlIOExceptionpublic static void readBySax(Reader reader, ContentHandler contentHandler) throws IOException
DefaultHandler,则其接口都会被处理reader - XML源Reader,使用后自动关闭contentHandler - XML流处理器,用于按照Element处理xmlIOExceptionpublic static void readBySax(InputStream source, ContentHandler contentHandler) throws IOException
DefaultHandler,则其接口都会被处理source - XML源流,使用后自动关闭contentHandler - XML流处理器,用于按照Element处理xmlIOExceptionpublic static void readBySax(InputSource source, ContentHandler contentHandler) throws IOException
DefaultHandler,则其接口都会被处理source - XML源,可以是文件、流、路径等contentHandler - XML流处理器,用于按照Element处理xmlIOExceptionpublic static Document parseXml(String xmlStr) throws IOException
xmlStr - XML字符串IOExceptionpublic static String toStr(Node doc) throws IOException
doc - XML文档IOExceptionpublic static String toStr(Document doc) throws IOException
doc - XML文档IOExceptionpublic static String toStr(Node doc, boolean isPretty) throws IOException
doc - XML文档isPretty - 是否格式化输出IOExceptionpublic static String toStr(Document doc, boolean isPretty) throws IOException
doc - XML文档isPretty - 是否格式化输出IOExceptionpublic static String toStr(Node doc, String charset, boolean isPretty) throws IOException
doc - XML文档charset - 编码isPretty - 是否格式化输出IOExceptionpublic static String toStr(Document doc, String charset, boolean isPretty) throws IOException
doc - XML文档charset - 编码isPretty - 是否格式化输出IOExceptionpublic static String toStr(Node doc, String charset, boolean isPretty, boolean omitXmlDeclaration) throws IOException
doc - XML文档charset - 编码isPretty - 是否格式化输出omitXmlDeclaration - 是否忽略 xml DeclarationIOExceptionpublic static String format(Document doc) throws IOException
doc - Document XML文档IOExceptionpublic static String format(String xmlStr) throws IOException
xmlStr - XML字符串IOExceptionpublic static void toFile(Document doc, String absolutePath) throws IOException
doc - XML文档absolutePath - 文件绝对路径,不存在会自动创建IOExceptionpublic static void toFile(Document doc, String path, String charsetName) throws IOException
doc - XML文档path - 文件路径charsetName - 自定义XML文件的编码,如果为null 读取XML文档中的编码,否则默认UTF-8IOExceptionpublic static void write(Node node, Writer writer, String charset, int indent) throws IOException
node - Node XML文档节点或文档本身writer - 写出的Writer,Writer决定了输出XML的编码charset - 编码indent - 格式化输出中缩进量,小于1表示不格式化输出IOExceptionpublic static void write(Node node, Writer writer, String charset, int indent, boolean omitXmlDeclaration) throws IOException
node - Node XML文档节点或文档本身writer - 写出的Writer,Writer决定了输出XML的编码charset - 编码indent - 格式化输出中缩进量,小于1表示不格式化输出omitXmlDeclaration - 是否输出 xml DeclarationIOExceptionpublic static void write(Node node, OutputStream out, String charset, int indent) throws IOException
node - Node XML文档节点或文档本身out - 写出的Writer,Writer决定了输出XML的编码charset - 编码indent - 格式化输出中缩进量,小于1表示不格式化输出IOExceptionpublic static void write(Node node, OutputStream out, String charset, int indent, boolean omitXmlDeclaration) throws IOException
node - Node XML文档节点或文档本身out - 写出的Writer,Writer决定了输出XML的编码charset - 编码indent - 格式化输出中缩进量,小于1表示不格式化输出omitXmlDeclaration - 是否输出 xml DeclarationIOExceptionpublic static void transform(Source source, Result result, String charset, int indent) throws IOException
source - 源result - 目标charset - 编码indent - 格式化输出中缩进量,小于1表示不格式化输出IOExceptionpublic static void transform(Source source, Result result, String charset, int indent, boolean omitXmlDeclaration) throws IOException
source - 源result - 目标charset - 编码indent - 格式化输出中缩进量,小于1表示不格式化输出omitXmlDeclaration - 是否输出 xml DeclarationIOExceptionpublic static Document createXml() throws IOException
IOExceptionpublic static DocumentBuilder createDocumentBuilder() throws IOException
IOExceptionpublic static DocumentBuilderFactory createDocumentBuilderFactory()
DocumentBuilderFactory
默认使用"com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl"
如果使用第三方实现,请调用disableDefaultDocumentBuilderFactory()
public static Document createXml(String rootElementName) throws IOException
rootElementName - 根节点名称IOExceptionpublic static Document createXml(String rootElementName, String namespace) throws IOException
rootElementName - 根节点名称namespace - 命名空间,无则传nullIOExceptionpublic static Element getRootElement(Document doc)
doc - DocumentDocument.getDocumentElement()public static Document getOwnerDocument(Node node)
node - 节点Documentpublic static String cleanInvalid(String xmlContent)
xmlContent - XML文本public static String cleanComment(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 - 父节点,如果指定将返回此节点的所有直接子节点,null返回所有就节点nodeList - NodeListpublic static void writeObjectAsXml(File dest, Object bean) throws IOException
dest - 目标文件bean - 对象IOExceptionpublic static XPath createXPath()
XPathpublic static Element getElementByXPath(String expression, Object source) throws IOException
expression - XPath表达式source - 资源,可以是Docunent、Node节点等IOExceptionpublic static NodeList getNodeListByXPath(String expression, Object source) throws IOException
expression - XPath表达式source - 资源,可以是Docunent、Node节点等IOExceptionpublic static Node getNodeByXPath(String expression, Object source) throws IOException
expression - XPath表达式source - 资源,可以是Docunent、Node节点等IOExceptionpublic static Object getByXPath(String expression, Object source, QName returnType) throws IOException
expression - XPath表达式source - 资源,可以是Docunent、Node节点等returnType - 返回类型,XPathConstantsIOExceptionpublic static Object getByXPath(String expression, Object source, QName returnType, NamespaceContext nsContext) throws IOException
expression - XPath表达式source - 资源,可以是Docunent、Node节点等returnType - 返回类型,XPathConstantsnsContext - NamespaceContextIOExceptionpublic static String escape(String string)
& (ampersand) 替换为 & < (小于) 替换为 < > (大于) 替换为 > " (双引号) 替换为 "
string - 被替换的字符串public static Map<String,Object> xmlToMap(String xmlStr) throws IOException
xmlStr - XML字符串IOExceptionpublic static <T> T xmlToBean(Node node, Class<T> bean)
T - bean类型node - XML节点bean - bean类public static Map<String,Object> xmlToMap(Node node)
node - XML节点public static Map<String,Object> xmlToMap(String xmlStr, Map<String,Object> result) throws IOException
xmlStr - XML字符串result - 结果Map类型IOExceptionpublic static Map<String,Object> xmlToMap(Node node, Map<String,Object> result)
node - XML节点result - 结果Map类型public static String mapToXmlStr(Map<?,?> data) throws IOException
data - Map类型数据IOExceptionpublic static String mapToXmlStr(Map<?,?> data, boolean omitXmlDeclaration) throws IOException
data - Map类型数据omitXmlDeclaration - 是否输出 xml DeclarationIOExceptionpublic static String mapToXmlStr(Map<?,?> data, String rootName) throws IOException
data - Map类型数据rootName - 根节点名IOExceptionpublic static String mapToXmlStr(Map<?,?> data, String rootName, String namespace) throws IOException
data - Map类型数据rootName - 根节点名namespace - 命名空间,可以为nullIOExceptionpublic static String mapToXmlStr(Map<?,?> data, String rootName, String namespace, boolean omitXmlDeclaration) throws IOException
data - Map类型数据rootName - 根节点名namespace - 命名空间,可以为nullomitXmlDeclaration - 是否输出 xml DeclarationIOExceptionpublic static String mapToXmlStr(Map<?,?> data, String rootName, String namespace, boolean isPretty, boolean omitXmlDeclaration) throws IOException
data - Map类型数据rootName - 根节点名namespace - 命名空间,可以为nullisPretty - 是否格式化输出omitXmlDeclaration - 是否输出 xml DeclarationIOExceptionpublic static String mapToXmlStr(Map<?,?> data, String rootName, String namespace, String charset, boolean isPretty, boolean omitXmlDeclaration) throws IOException
data - Map类型数据rootName - 根节点名namespace - 命名空间,可以为nullcharset - 编码isPretty - 是否格式化输出omitXmlDeclaration - 是否输出 xml DeclarationIOExceptionpublic static Document mapToXml(Map<?,?> data, String rootName) throws IOException
data - Map类型数据rootName - 根节点名IOExceptionpublic static Document mapToXml(Map<?,?> data, String rootName, String namespace) throws IOException
data - Map类型数据rootName - 根节点名namespace - 命名空间,可以为nullIOExceptionpublic static Document beanToXml(Object bean) throws IOException
bean - Bean对象IOExceptionpublic static Document beanToXml(Object bean, String namespace) throws IOException
bean - Bean对象namespace - 命名空间,可以为nullIOExceptionpublic static Document beanToXml(Object bean, String namespace, boolean ignoreNull) throws IOException
bean - Bean对象namespace - 命名空间,可以为nullignoreNull - 忽略值为null的属性IOExceptionpublic static boolean isElement(Node node)
Element 类型节点node - 节点Element 类型节点public static Element appendChild(Node node, String tagName)
node - 节点tagName - 标签名public static Element appendChild(Node node, String tagName, String namespace)
node - 节点tagName - 标签名namespace - 命名空间,无传nullpublic static Node appendText(Node node, CharSequence text)
node - 节点text - 文本Copyright © 2025 fossc. All rights reserved.