Java中有与Ruby的Nokogiri :: XML :: EntityDecl等效的东西吗? [英] Is there some equivalent in Java to Ruby's Nokogiri::XML::EntityDecl?

查看:88
本文介绍了Java中有与Ruby的Nokogiri :: XML :: EntityDecl等效的东西吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个问题,我需要在包含定义中括号内的ENTITY的DTD之前添加到现有XML文档中。

I have an issue where I need to prepend a DTD containing ENTITYs bracketed in the definition to an existing XML document.

例如,根据MathML的规范进行工作在DAISY中的 http://www.daisy.org/projects/ mathml / mathml-in-daisy-spec.html ,说我是由外部来源提供此XML的:

For example, working from the specification for MathML in DAISY at http://www.daisy.org/projects/mathml/mathml-in-daisy-spec.html, say I am given this XML by an outside source:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dtbook PUBLIC "-//NISO//DTD dtbook 2005-2//EN"
 "http://www.daisy.org/z3986/2005/dtbook-2005-2.dtd">
<dtbook xmlns="http://www.daisy.org/z3986/2005/dtbook/" xmlns:m="http://www.w3.org/1998/Math/MathML"
    version="2005-3" xml:lang="eng">
    <m:math xmlns:dtbook="http://www.daisy.org/z3986/2005/dtbook/"
  id="math0001" dtbook:smilref="nativemathml.smil#math0001"
  altimg="nativemathml0001.png"
  alttext="sigma-summation UnderScript i equals zero OverScript infinity EndScripts x Subscript i">
      <m:mrow>
        <m:mstyle displaystyle='true'>
          <m:munderover>
            <m:mo>&#x2211;</m:mo>
            <m:mrow>
              <m:mi>i</m:mi><m:mo>=</m:mo><m:mn>0</m:mn>
            </m:mrow>
            <m:mi>&#x221E;</m:mi>
          </m:munderover>
          <m:mrow>
            <m:msub>
              <m:mi>x</m:mi>
              <m:mi>i</m:mi>
            </m:msub>
          </m:mrow>
        </m:mstyle>
      </m:mrow>
</m:math>
</dtbook>

我想从规范中添加ENTITY定义,以使本书支持MathML,从而使结果看起来像这样:

I want to add the ENTITY definitions from the specification to make this book support MathML, so that the result looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE dtbook PUBLIC "-//NISO//DTD dtbook 2005-2//EN"
 "http://www.daisy.org/z3986/2005/dtbook-2005-2.dtd"
 [
  <!ENTITY % MATHML.prefixed "INCLUDE" >
  <!ENTITY % MATHML.prefix "m">
  <!ENTITY % MATHML.Common.attrib
          "xlink:href    CDATA       #IMPLIED
          xlink:type     CDATA       #IMPLIED
          class          CDATA       #IMPLIED
          style          CDATA       #IMPLIED
          id             ID          #IMPLIED
          xref           IDREF       #IMPLIED
          other          CDATA       #IMPLIED
          xmlns:dtbook   CDATA       #FIXED 'http://www.daisy.org/z3986/2005/dtbook/'
          dtbook:smilref CDATA       #IMPLIED"
  >
  <!ENTITY % mathML2 PUBLIC "-//W3C//DTD MathML 2.0//EN"
             "http://www.w3.org/Math/DTD/mathml2/mathml2.dtd"
  >
  %mathML2;
  <!ENTITY % externalFlow "| m:math">
  <!ENTITY % externalNamespaces "xmlns:m CDATA #FIXED
    'http://www.w3.org/1998/Math/MathML'">
 ]
>
<dtbook xmlns="http://www.daisy.org/z3986/2005/dtbook/" xmlns:m="http://www.w3.org/1998/Math/MathML"
    version="2005-3" xml:lang="eng">
    <m:math xmlns:dtbook="http://www.daisy.org/z3986/2005/dtbook/"
  id="math0001" dtbook:smilref="nativemathml.smil#math0001"
  altimg="nativemathml0001.png"
  alttext="sigma-summation UnderScript i equals zero OverScript infinity EndScripts x Subscript i">
      <m:mrow>
        <m:mstyle displaystyle='true'>
          <m:munderover>
            <m:mo>&#x2211;</m:mo>
            <m:mrow>
              <m:mi>i</m:mi><m:mo>=</m:mo><m:mn>0</m:mn>
            </m:mrow>
            <m:mi>&#x221E;</m:mi>
          </m:munderover>
          <m:mrow>
            <m:msub>
              <m:mi>x</m:mi>
              <m:mi>i</m:mi>
            </m:msub>
          </m:mrow>
        </m:mstyle>
      </m:mrow>
</m:math>
</dtbook>

在Ruby中,Nokogiri中有一个方法可以用来添加这些看起来像下面的ENTITY定义此:
Nokogiri :: XML :: EntityDecl.new( MATHML.prefixed,doc,MATHML_ENTITY_DECL_TYPE,nil,nil, INCLUDE)

In Ruby, there is a method in Nokogiri that can be used to add these ENTITY definitions that looks like this: Nokogiri::XML::EntityDecl.new("MATHML.prefixed", doc, MATHML_ENTITY_DECL_TYPE, nil, nil, "INCLUDE")

是在Java中有与此等效的东西吗?我们正在使用JDOM处理XML文档,但是JDOM DocType类似乎不支持这些实体定义。

Is there an equivalent to this in Java? We are using JDOM to manipulate our XML documents, but the JDOM DocType class doesn't appear to support these entity definitions.

推荐答案

使用JDOM,您应该能够解析原始文档,并从文档中拉出DTDContent节点。

With JDOM you should be able to parse the original document, and pull the DTDContent node from the document.

您的代码将类似于:

Document doc = saxBuilder.build(myxmlfile);
DocType dtd = doc.getDocType();

dtd 内容应为 dtbook 引用。

您现在可以使用mathml声明的字符串表示形式,并将它们作为一个DocType的内部子集(也许您想从文件中读取它,或者作为系统资源或其他内容来读取它。)

You can now take the string-representation of the mathml declarations, and include them as an internalsubset of the DocType (perhaps you want to read it from a file, or as a System resource, or something).

String internal = "  <!ENTITY % MATHML.prefixed \"INCLUDE\" >\n"
    + "  <!ENTITY % MATHML.prefix \"m\">\n"
    + ......

dtd.setInternalSubset(internal);

请参阅: http://www.jdom.org/docs/apidocs/org/jdom2/DocType.html#setInternalSubset(java.lang .String)

这将修改声明,并且,如果输出XML,则应具有期望的内容:

This will modify the declaration, and, if you output the XML, you should have the content you expect:

XMLOutputter xout = new XMLOutputter();
xout.output(doc, System.out);

这篇关于Java中有与Ruby的Nokogiri :: XML :: EntityDecl等效的东西吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆