news 2026/6/13 12:08:00

Java 标准 JAXP(Java API for XML Processing),JDK 内置,无需额外引入第三方依赖

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
Java 标准 JAXP(Java API for XML Processing),JDK 内置,无需额外引入第三方依赖

一、包完整信息

包路径javax.xml.transform.dom
归属:Java 标准 JAXP(Java API for XML Processing),JDK 内置,无需额外引入第三方依赖。

二、核心作用

提供 DOM(Document Object Model)树与 XSLT 转换接口javax.xml.transform之间的桥梁:

  1. 把 DOMNode/Document封装成 Transformer 可读取的源数据(Source)
  2. 把 XSLT 转换结果直接输出写入 DOM 节点(Result)

三、关键常用类一览

1. DOMSource

javax.xml.transform.dom.DOMSource
  • 用途:将 DOM 节点(Document、Element 等)包装成Source,供Transformer.transform()读取 XML 输入。
  • 构造:
    newDOMSource(document);newDOMSource(node,systemId);

2. DOMResult

javax.xml.transform.dom.DOMResult
  • 用途:将 XSLT 转换结果写入一个 DOM Node,不再输出到文件/流,直接得到新 DOM 树。
  • 构造:
    newDOMResult(newDocumentNode);

3. DOMLocator

javax.xml.transform.dom.DOMLocator

扩展标准Locator,额外返回出错对应的 DOM 节点,用于 XSLT 转换异常定位具体 DOM 元素。

四、典型代码示例(DOM → XSLT → 新DOM)

importorg.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文档DocumentsrcDoc=DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();// 2. DOMSource 包装源DOMDOMSourcesource=newDOMSource(srcDoc);// 3. 新建空白DOM用于接收转换结果DocumenttargetDoc=DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();DOMResultresult=newDOMResult(targetDoc);// 4. 执行XSLT转换,结果直接写入targetDocTransformertransformer=TransformerFactory.newInstance().newTransformer();transformer.transform(source,result);// targetDoc 就是转换后的完整DOM树}}

五、版本与模块化说明

  1. Java 8 及更早:默认 rt.jar 自带,直接 import 使用。
  2. Java 9+ 模块化(JPMS)
    该包位于模块java.xml,模块声明需添加:
    requires java.xml;
  3. 注意:高版本 JDK 逐步收紧 XML 外部实体安全,使用 Transformer 建议配置防 XXE 防护。

六、配套对照关系

源/结果类型对应实现类适用场景
内存DOM树输入DOMSource已有DOM对象,不读磁盘文件
输出到内存DOMDOMResult转换后继续用DOM API操作节点
文件输入StreamSource读取本地XML/XSL文件
输出到文件StreamResult直接生成XML磁盘文件

七、常见使用场景

  1. 内存中修改 XML DOM 后,用 XSLT 样式表做格式化、字段映射、结构重构;
  2. 无需落地临时 XML 文件,全程内存 DOM 流转,性能更高;
  3. XML 报文接口收发时,DOM 对象与 XSLT 批量转换。
    Package javax.xml.transform.dom
    This package implements DOM-specific transformation APIs.

See: Description

Interface 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 Description

This 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.

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/6/13 12:04:56

MC68328并行端口配置详解:从寄存器操作到中断控制实战

1. 项目概述与核心价值在嵌入式系统开发领域,尤其是针对像MC68328这类经典的Motorola DragonBall系列微处理器,并行端口的配置是连接软件与硬件的桥梁,也是驱动工程师必须啃下的硬骨头。很多新手在面对芯片手册里密密麻麻的寄存器描述时&…

作者头像 李华
网站建设 2026/6/13 12:03:08

如何彻底解决Mac多设备滚动混乱?Scroll Reverser的终极指南

如何彻底解决Mac多设备滚动混乱?Scroll Reverser的终极指南 【免费下载链接】Scroll-Reverser Per-device scrolling prefs on macOS. 项目地址: https://gitcode.com/gh_mirrors/sc/Scroll-Reverser 你是否曾经遇到过这样的烦恼:在MacBook触控板…

作者头像 李华
网站建设 2026/6/13 12:00:20

5分钟上手Mousecape:macOS光标定制终极指南

5分钟上手Mousecape:macOS光标定制终极指南 【免费下载链接】Mousecape Cursor Manager for OSX 项目地址: https://gitcode.com/gh_mirrors/mo/Mousecape 想让你的Mac电脑拥有独一无二的鼠标指针体验吗?Mousecape作为macOS平台上专业的光标定制神…

作者头像 李华
网站建设 2026/6/13 12:00:19

3步快速解决线缆依赖问题:NoCableLauncher的完整使用指南

3步快速解决线缆依赖问题:NoCableLauncher的完整使用指南 【免费下载链接】NoCableLauncher Rocksmith 2014 Launcher for playing without RealTone cable (nocable fix) 项目地址: https://gitcode.com/gh_mirrors/no/NoCableLauncher 还在为Rocksmith 201…

作者头像 李华