首页 > 解决方案 > 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,就可以完全按照原样重新编写它们”。

标签: javaxmljaxbreqif

解决方案



推荐阅读