一、包完整信息包路径javax.xml.transform.dom归属Java 标准 JAXPJava API for XML ProcessingJDK 内置无需额外引入第三方依赖。二、核心作用提供 DOMDocument Object Model树与 XSLT 转换接口javax.xml.transform之间的桥梁把 DOMNode/Document封装成 Transformer 可读取的源数据Source把 XSLT 转换结果直接输出写入 DOM 节点Result三、关键常用类一览1. DOMSourcejavax.xml.transform.dom.DOMSource用途将 DOM 节点Document、Element 等包装成Source供Transformer.transform()读取 XML 输入。构造newDOMSource(document);newDOMSource(node,systemId);2. DOMResultjavax.xml.transform.dom.DOMResult用途将 XSLT 转换结果写入一个 DOM Node不再输出到文件/流直接得到新 DOM 树。构造newDOMResult(newDocumentNode);3. DOMLocatorjavax.xml.transform.dom.DOMLocator扩展标准Locator额外返回出错对应的 DOM 节点用于 XSLT 转换异常定位具体 DOM 元素。四、典型代码示例DOM → XSLT → 新DOMimportorg.w3c.dom.Document;importjavax.xml.parsers.DocumentBuilderFactory;importjavax.xml.transform.Transformer;importjavax.xml.transform.TransformerFactory;importjavax.xml.transform.dom.DOMSource;importjavax.xml.transform.dom.DOMResult;publicclassXmlDomTransformDemo{publicstaticvoidmain(String[]args)throwsException{// 1. 构造原始DOM文档DocumentsrcDocDocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();// 2. DOMSource 包装源DOMDOMSourcesourcenewDOMSource(srcDoc);// 3. 新建空白DOM用于接收转换结果DocumenttargetDocDocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();DOMResultresultnewDOMResult(targetDoc);// 4. 执行XSLT转换结果直接写入targetDocTransformertransformerTransformerFactory.newInstance().newTransformer();transformer.transform(source,result);// targetDoc 就是转换后的完整DOM树}}五、版本与模块化说明Java 8 及更早默认 rt.jar 自带直接 import 使用。Java 9 模块化JPMS该包位于模块java.xml模块声明需添加requires java.xml;注意高版本 JDK 逐步收紧 XML 外部实体安全使用 Transformer 建议配置防 XXE 防护。六、配套对照关系源/结果类型对应实现类适用场景内存DOM树输入DOMSource已有DOM对象不读磁盘文件输出到内存DOMDOMResult转换后继续用DOM API操作节点文件输入StreamSource读取本地XML/XSL文件输出到文件StreamResult直接生成XML磁盘文件七、常见使用场景内存中修改 XML DOM 后用 XSLT 样式表做格式化、字段映射、结构重构无需落地临时 XML 文件全程内存 DOM 流转性能更高XML 报文接口收发时DOM 对象与 XSLT 批量转换。Package javax.xml.transform.domThis package implements DOM-specific transformation APIs.See: DescriptionInterface Summary Interface Description DOMLocator Indicates the position of a node in a source DOM, intended primarily for error reporting. Class Summary Class Description DOMResult Acts as a holder for a transformation result tree in the form of a Document Object Model (DOM) tree. DOMSource Acts as a holder for a transformation Source tree in the form of a Document Object Model (DOM) tree.Package javax.xml.transform.dom DescriptionThis package implements DOM-specific transformation APIs.The DOMSource class allows the client of the implementation of this API to specify a DOM Node as the source of the input tree. The model of how the Transformer deals with the DOM tree in terms of mismatches with the XSLT data model or other data models is beyond the scope of this document. Any of the nodes derived from Node are legal input.The DOMResult class allows a Node to be specified to which result DOM nodes will be appended. If an output node is not specified, the transformer will use DocumentBuilder.newDocument() to create an output Document node. If a node is specified, it should be one of the following: Document, Element, or DocumentFragment. Specification of any other node type is implementation dependent and undefined by this API. If the result is a Document, the output of the transformation must have a single element root to set as the document element.The DOMLocator node may be passed to TransformerException objects, and retrieved by trying to cast the result of the TransformerException.getLocator() method. The implementation has no responsibility to use a DOMLocator instead of a SourceLocator (though line numbers and the like do not make much sense for a DOM), so the result of getLocator must always be tested with an instanceof.
Java 标准 JAXP(Java API for XML Processing),JDK 内置,无需额外引入第三方依赖
一、包完整信息包路径javax.xml.transform.dom归属Java 标准 JAXPJava API for XML ProcessingJDK 内置无需额外引入第三方依赖。二、核心作用提供 DOMDocument Object Model树与 XSLT 转换接口javax.xml.transform之间的桥梁把 DOMNode/Document封装成 Transformer 可读取的源数据Source把 XSLT 转换结果直接输出写入 DOM 节点Result三、关键常用类一览1. DOMSourcejavax.xml.transform.dom.DOMSource用途将 DOM 节点Document、Element 等包装成Source供Transformer.transform()读取 XML 输入。构造newDOMSource(document);newDOMSource(node,systemId);2. DOMResultjavax.xml.transform.dom.DOMResult用途将 XSLT 转换结果写入一个 DOM Node不再输出到文件/流直接得到新 DOM 树。构造newDOMResult(newDocumentNode);3. DOMLocatorjavax.xml.transform.dom.DOMLocator扩展标准Locator额外返回出错对应的 DOM 节点用于 XSLT 转换异常定位具体 DOM 元素。四、典型代码示例DOM → XSLT → 新DOMimportorg.w3c.dom.Document;importjavax.xml.parsers.DocumentBuilderFactory;importjavax.xml.transform.Transformer;importjavax.xml.transform.TransformerFactory;importjavax.xml.transform.dom.DOMSource;importjavax.xml.transform.dom.DOMResult;publicclassXmlDomTransformDemo{publicstaticvoidmain(String[]args)throwsException{// 1. 构造原始DOM文档DocumentsrcDocDocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();// 2. DOMSource 包装源DOMDOMSourcesourcenewDOMSource(srcDoc);// 3. 新建空白DOM用于接收转换结果DocumenttargetDocDocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();DOMResultresultnewDOMResult(targetDoc);// 4. 执行XSLT转换结果直接写入targetDocTransformertransformerTransformerFactory.newInstance().newTransformer();transformer.transform(source,result);// targetDoc 就是转换后的完整DOM树}}五、版本与模块化说明Java 8 及更早默认 rt.jar 自带直接 import 使用。Java 9 模块化JPMS该包位于模块java.xml模块声明需添加requires java.xml;注意高版本 JDK 逐步收紧 XML 外部实体安全使用 Transformer 建议配置防 XXE 防护。六、配套对照关系源/结果类型对应实现类适用场景内存DOM树输入DOMSource已有DOM对象不读磁盘文件输出到内存DOMDOMResult转换后继续用DOM API操作节点文件输入StreamSource读取本地XML/XSL文件输出到文件StreamResult直接生成XML磁盘文件七、常见使用场景内存中修改 XML DOM 后用 XSLT 样式表做格式化、字段映射、结构重构无需落地临时 XML 文件全程内存 DOM 流转性能更高XML 报文接口收发时DOM 对象与 XSLT 批量转换。Package javax.xml.transform.domThis package implements DOM-specific transformation APIs.See: DescriptionInterface Summary Interface Description DOMLocator Indicates the position of a node in a source DOM, intended primarily for error reporting. Class Summary Class Description DOMResult Acts as a holder for a transformation result tree in the form of a Document Object Model (DOM) tree. DOMSource Acts as a holder for a transformation Source tree in the form of a Document Object Model (DOM) tree.Package javax.xml.transform.dom DescriptionThis package implements DOM-specific transformation APIs.The DOMSource class allows the client of the implementation of this API to specify a DOM Node as the source of the input tree. The model of how the Transformer deals with the DOM tree in terms of mismatches with the XSLT data model or other data models is beyond the scope of this document. Any of the nodes derived from Node are legal input.The DOMResult class allows a Node to be specified to which result DOM nodes will be appended. If an output node is not specified, the transformer will use DocumentBuilder.newDocument() to create an output Document node. If a node is specified, it should be one of the following: Document, Element, or DocumentFragment. Specification of any other node type is implementation dependent and undefined by this API. If the result is a Document, the output of the transformation must have a single element root to set as the document element.The DOMLocator node may be passed to TransformerException objects, and retrieved by trying to cast the result of the TransformerException.getLocator() method. The implementation has no responsibility to use a DOMLocator instead of a SourceLocator (though line numbers and the like do not make much sense for a DOM), so the result of getLocator must always be tested with an instanceof.