【问题标题】:DTD Element Type to accept '<'接受 '<' 的 DTD 元素类型
【发布时间】:2015-03-17 23:03:30
【问题描述】:

我的 xml 文件可能如下所示:

<unclassified>
  WOOD FIRM FINED #30,000 OVER TEEN'S LOST ARM<
</unclassified>

.dtd 声明:

<!ELEMENT unclassified   (#PCDATA)>

不幸的是,这似乎不起作用,因为我总是收到这样的错误:

[Fatal Error] arm1sub.sgml:14:46: The content of elements must consist of well-formed character data or markup.
org.xml.sax.SAXParseException; systemId: file:/home/sfalk/workspace/project/target/classes/meter_corpus/PA/annotated/courts/12.07.99/arm/arm1sub.sgml; lineNumber: 14; columnNumber: 46; The content of elements must consist of well-formed character data or markup.
    at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(DOMParser.java:257)
    at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(DocumentBuilderImpl.java:348)
    at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:205)

我怎样才能做到这一点?我希望这在不操纵我的 .xml 文件的情况下是可行的..

【问题讨论】:

标签: java xml dtd


【解决方案1】:

您无法更改 DTD 中的任何内容来解决此问题。必须更改“XML”文档本身。 (从技术上讲,您的文档甚至不是真正的 XML。)

DTD(和 XSD)的权限是验证,但 XML 有效的先决条件是它格式正确。 (事实上​​,文档成为 XML 的先决条件是它格式正确。)

阅读Well-formed vs Valid XML 以获得对差异的全面解释。对于您的特定问题,&amp;lt; 替换为&amp;lt; 以使您的XML格式正确。

【讨论】:

    【解决方案2】:

    如果您想为 xml 解析器使用包含无效字符的值,您可以使用 CDATA:http://www.w3schools.com/xml/xml_cdata.asp

    <unclassified>
      <![CDATA[WOOD FIRM FINED #30,000 OVER TEEN'S LOST ARM<]]>
    </unclassified>
    

    或者说是你放的比你不想放的低……

    <unclassified>
      WOOD FIRM FINED #30,000 OVER TEEN'S LOST ARM
    </unclassified>
    

    【讨论】:

      猜你喜欢
      • 2012-07-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-06
      • 1970-01-01
      • 2011-12-15
      相关资源
      最近更新 更多