Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
重写XML文件时使用哪种Java XML解析方法?_Java_Xml_Xpath_Sax_Entities - Fatal编程技术网

重写XML文件时使用哪种Java XML解析方法?

重写XML文件时使用哪种Java XML解析方法?,java,xml,xpath,sax,entities,Java,Xml,Xpath,Sax,Entities,为了清晰起见进行了编辑 我正在编写一个Java应用程序,它接受一个XML文件,并在需要更新文件中的信息时重写它。XML文件的示例如下所示: <!DOCTYPE book PUBLIC "myDTD.dtd" [ <!ENTITY % ent SYSTEM "entities.ent"> %ent; ]> <book id="EXDOC" label="beta" lang="en"> <title>Example Document<

为了清晰起见进行了编辑

我正在编写一个Java应用程序,它接受一个XML文件,并在需要更新文件中的信息时重写它。XML文件的示例如下所示:

<!DOCTYPE book PUBLIC "myDTD.dtd" [

<!ENTITY % ent SYSTEM "entities.ent">
%ent;

]>

<book id="EXDOC" label="beta" lang="en">
   <title>Example Document</title>
   <bookinfo>
      <authorgroup>
         <author>
            <firstname>George</firstname>
            <surname>Washington</surname>
         </author>
         <author>
            <firstname>Barbara</firstname>
            <surname>Bush</surname>
         </author>
      </authorgroup>
      <pubsnumber>E12345</pubsnumber>
      <releaseinfo/>
      <pubdate>March 2016</pubdate>
      <copyright>
         <year>2012, 2016</year>
         <holder>Company and/or its affiliates. All rights reserved.</holder>
      </copyright>
      <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="Abstract.xml" parse="xml"/>
      <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="legal.xml" parse="xml"/>
   </bookinfo>
   <xi:include xmlns:xi="http://www.w3.org/2001/XInclude" href="preface.xml" parse="xml"/>
...
不过,这带来了一些限制,我真的很想绕过这些限制。首先,如果内联文本具有文本实体&something;,我希望实体保持原样。此时,我的实体在重写文件时解析为文本本身

例如,如果我有

<!ENTITY something "Something">

如果我的文件包含以下内容:

<para> There's a &something; here.</para>
有一个&something;在这里
当我重写时,我希望它说:

<para> Here's a &something; there.</para>
这里有一个&something;那里
但实体解析后,文件变为:

<para>Here's a Something there.</para>
这里有个东西。
我不确定如何处理entityResolver类,这样在我读取节点时,它不会自动解析这些实体,而不会中断其余代码。我还有另一个与XPATH一起使用的类,它从文档中提取某些信息,将XML文件中的信息与数据库中记录的信息进行比较,因此我不能不设置entityResolver,否则XPATH表达式会完全崩溃

我想我可以有一个单独的解析器来读/写XML文件,然后是SAX解析器,这是从我们的数据库中获取信息所必需的,但我希望尽可能干净地完成这项工作


非常感谢您的帮助……

不幸的是,您无法告诉转换引擎不要扩展实体引用。这是在解析XML时发生的,因此在转换XML内容时,它们将丢失

对于多阶段转型场景,您可以:

  • 用实体引用(如标记)替换实体引用,即替换
    &something与<代码>一起
    
  • 执行转换以根据需要调整内容,这不会扩展实体引用,并且会像标记一样保留实体引用。如果您确实需要解析实体以验证这些实体信息,还可以加载原始XML文档(带有扩展实体)并在文档之间进行交叉引用

  • 使用另一个find/replace将转换输出中的实体引用(如标记)更改回实体引用


  • 您应该为this.EJP使用XSLT—在对数据库进行检查时XSLT可用吗?我以前从未使用过它,所以其他信息会很有用。嗨,Mads,谢谢你的回复。不幸的是,实体在被读取之前实际上已被解析。这是因为实体未存储在数据库中。例如,如果我有一个&ProductName,其中&ProductName;解析为“公司新产品名称”,数据库中的所有内容都是“公司新产品名称”。所以我不能使用实体标记。经过一些融资后,在我解析文档之前使用引用标记非常有效!谢谢Mads!
    <para>Here's a Something there.</para>