当前位置: 首页 > 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.zskr.cn/news/1516957.html

相关文章:

  • netstat命令和ss命令详解
  • PythonVista:突破系统限制,为老旧Windows重新定义Python兼容性边界
  • 2026运城旧金铂银回收黄金回收高信誉门店汇总 5 家线下实体回收商家实地评测与联络渠道整理 - 中业金奢再生回收中心
  • 硬件工程师踩过的坑:Buck电路PCB布局的10个细节(附AD/嘉立创实战案例)
  • 路灯智能控制模块怎么选型?看光控时控经纬度远程四大功能
  • FPGA实战(07): Verilog 实现带符号输出的 0~99 循环计数器(tops 模块)设计与仿真
  • PyTorch-NPU/stable-diffusion-2-1:华为NPU优化的AI绘画模型完全指南 [特殊字符]
  • 基于plc的楼宇供电控制系统及综合防雷设计23(设计源文件+万字报告+讲解)(支持资料、图片参考_相关定制)_文章底部可以扫码
  • 2026大同市卡地亚+GP芝柏表手表专业回收,26年精选回收店铺排行榜推荐 - 莘州文化
  • 团队API文档难维护?怎么用 Claude 快速生成 Markdown?一文看懂选型与实战指南
  • 2026年北京财务管理公司对比测评,代理记账机构哪家强? - 互联百晓生
  • 2026伊犁市百达翡丽+宝珀手表专业回收,26年精选回收店铺排行榜推荐 - 莘州文化
  • 基于PLC控制的铺丝机总体及其控制系统设计23(设计源文件+万字报告+讲解)(支持资料、图片参考_相关定制)_文章底部可以扫码
  • 数据分析师的肌肉记忆:原始数据诊断四层校验法
  • 告别信号衰减!手把手教你制作7/8馈线接头(附工具清单与防短路技巧)
  • M68000处理器数据格式详解:从整数到浮点数的底层表示与对齐优化
  • 嵌入式以太网驱动深度解析:从ENET硬件到SDK实战
  • FPGA实战(10):FPGA全流水复数乘法器设计及自动化验证(Verilog)
  • 终极指南:三步快速解锁原神60FPS限制,享受丝滑游戏体验
  • 经验分享:2026京东 E 卡回收常见骗局拆解与安全交易方案 - 京卡收卡券回收
  • 2026年上海采购新人CPPM报名前需要准备什么?众智商学院官网入门条件与资料清单确认 - 众智商学院职业教育
  • 手机必备的百宝箱 !装机必备的多功能工具app!一站式解决你的日常小需求
  • 2026免费微信投票制作系统推荐:火星投票快速上手攻略,批量导入+强防刷 - 微信投票小程序
  • 如何3步突破私有知识库部署瓶颈:实战AnythingLLM全流程指南
  • WPF流程图编辑器源码:拖拽建模、连线交互、实时属性调整
  • 2026 年 6 月深圳卡地亚首饰回收,专柜成套饰品统一收,专业鉴品估值客观公道 - 薛定谔的梨花猫
  • 百联 OK 卡回收 闲置卡券变现实用指南 - 团团收购物卡回收
  • 2026手把手教你用手机免费做大一寸证件照,附尺寸参数+完整生成教程 - 办公小帮手
  • OpenCore Legacy Patcher深度探索:让旧款Mac焕发新生的完整实战指南
  • 2026巴音郭楞市欧米茄+宇航手表专业回收,26年精选回收店铺排行榜推荐 - 莘州文化