java - JAXB ~ 动态解析多个命名空间
问题描述
我正在尝试创建一个适用于以下 XML 文件的解组器:
<?xml version="1.0" encoding="UTF-8"?>
<REQ-IF xmlns="http://www.omg.org/spec/ReqIF/20110401/reqif.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.omg.org/spec/ReqIF/20110401/reqif.xsd
xml:lang="en">
[...]
</REQ-IF>
<?xml version="1.0" encoding="UTF-8"?>
<REQ-IF xmlns="http://www.omg.org/spec/ReqIF/20110401/reqif.xsd"
xmlns:configuration="http://eclipse.org/rmf/pror/toolextensions/1.0"
xmlns:id="http://pror.org/presentation/id"
xmlns:xhtml="http://www.w3.org/1999/xhtml">
[...]
</REQ-IF>
<?xml version="1.0" encoding="UTF-8"?>
<REQ-IF xmlns="http://www.omg.org/spec/ReqIF/20110401/reqif.xsd"
xmlns:doors="http://www.ibm.com/rdm/doors/REQIF/xmlns/1.0"
xmlns:reqif="http://www.omg.org/spec/ReqIF/20110401/reqif.xsd"
xmlns:reqif-common="http://www.prostep.org/reqif"
xmlns:reqif-xhtml="http://www.w3.org/1999/xhtml"
xmlns:rm="http://www.ibm.com/rm"
xmlns:rm-reqif="http://www.ibm.com/rm/reqif"
xmlns:xhtml="http://www.w3.org/1999/xhtml"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
[...]
</REQ-IF>
所有这些文件在结构上都是相同的,并且基于相同的顶级命名空间,但还包含各种可变的子级命名空间和其他“东西”(根据我的理解应该是属性,但不是),需要保存在系统中。
到目前为止,我已经设法节省了这么多:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<REQ-IF xmlns="http://www.omg.org/spec/ReqIF/20110401/reqif.xsd">
[...]
</REQ-IF>
但是,我的预期结果将如下所示:
<?xml version="1.0" encoding="UTF-8"?>
<REQ-IF xmlns="http://www.omg.org/spec/ReqIF/20110401/reqif.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.omg.org/spec/ReqIF/20110401/reqif.xsd
xml:lang="en">
[...]
</REQ-IF>
所以顶级命名空间被保存了,但在导入/导出过程中,子级命名空间和其他“东西”丢失了。这是不好的。
考虑到它们是动态生成的,如何保存其他子命名空间和其他“事物”?
基本上,我想说的是“在解析 XML 时以任何你喜欢的方式保存所有这些额外的属性,并且一旦你再次导出 XML,就可以完全按照原样重新编写它们”。
解决方案
如果您的主要用例是读取 ReqIF 文件,请考虑在https://www.eclipse.org/rmf/上有一个 ReqIF (de)serializer 的开源实现
推荐阅读
- c# - Func<> 动态添加一个额外的参数
- html - 使用 AngularJS 弹出直方图窗口
- javascript - 使用 socket.io 接收事件并更新元素 html
- php - Preg_matching 电子邮件并查找特定文本
- javascript - 使用JS更改输入字段的背景颜色
- angular - TypeScript/Angular: Logging instance variable
- javascript - 在 vuex 中创建动作的正确方法
- java - Eclipse Glassfish/Payara 无法识别我的 JDK
- r - 自相关函数的循环模拟
- angular - Angular - Ionic - 无法创建弹出框,组件注入错误