当前位置: 首页 > news >正文

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.

http://www.jsqmd.com/news/1005177/

相关文章:

  • 嵌入式设备日志自动备份:用Dropbear+SCP免密传输,5分钟搞定脚本配置
  • 3大核心技术革新:MAA明日方舟助手如何实现全日常一键长草
  • netstat命令和ss命令详解
  • PythonVista:突破系统限制,为老旧Windows重新定义Python兼容性边界
  • 2026年高校学生财务入门类证书推荐
  • 开封市杞县2026有实力的叛逆孩子学校哪家好?口碑好的叛逆少年学校选购指南与真实对比 - 善良的阿良
  • 硬件工程师踩过的坑:Buck电路PCB布局的10个细节(附AD/嘉立创实战案例)
  • 链表解题总结
  • 2026运城旧金铂银回收黄金回收高信誉门店汇总 5 家线下实体回收商家实地评测与联络渠道整理 - 中业金奢再生回收中心
  • M68000浮点指令集:从IEEE 754标准到硬件/软件协同设计
  • NXP ISF v2.2框架解析:嵌入式传感器驱动标准化与Kinetis实战
  • [特殊字符]‍♂️每天20分钟间歇跑,跑掉“内脏脂肪”,收获平坦小腹!
  • 2026甄选:宁波奢侈品回收专业服务公司,包包/二手表/首饰回收的估值与安全标杆 - 品牌发掘
  • 路灯智能控制模块怎么选型?看光控时控经纬度远程四大功能
  • 2026西双版纳旧金铂银回收黄金回收高信誉门店汇总 5 家线下实体回收商家实地评测与联络渠道整理 - 中业金奢再生回收中心
  • 2026新疆旧金铂银回收黄金回收高信誉门店汇总 5 家线下实体回收商家实地评测与联络渠道整理 - 中业金奢再生回收中心
  • TDA4VM实战:如何用它快速搭建一个ADAS原型系统(含传感器融合思路)
  • 第五卷:方程兵器谱(代数学)
  • FPGA实战(07): Verilog 实现带符号输出的 0~99 循环计数器(tops 模块)设计与仿真
  • PyTorch-NPU/stable-diffusion-2-1:华为NPU优化的AI绘画模型完全指南 [特殊字符]
  • Wand-Enhancer:为游戏爱好者打造的本地化WeMod增强解决方案
  • 基于plc的楼宇供电控制系统及综合防雷设计23(设计源文件+万字报告+讲解)(支持资料、图片参考_相关定制)_文章底部可以扫码
  • Anthropic删除推理网关层:编译时模型绑定实现GPU直连
  • 新闻语料工程实践:轻量级NLP新闻清洗与结构化方案
  • 基于PLC控制的铺丝机总体及其控制系统设计23(设计源文件+万字报告+讲解)(支持资料、图片参考_相关定制)_文章底部可以扫码
  • 2026伊犁市百达翡丽+宝珀手表专业回收,26年精选回收店铺排行榜推荐 - 莘州文化
  • 2026年北京财务管理公司对比测评,代理记账机构哪家强? - 互联百晓生
  • 2026年保定财务管理公司哪家强?代理记账服务对比测评 - 互联百晓生
  • 2026大同市卡地亚+GP芝柏表手表专业回收,26年精选回收店铺排行榜推荐 - 莘州文化
  • 团队API文档难维护?怎么用 Claude 快速生成 Markdown?一文看懂选型与实战指南