【问题标题】:How to solve the exception when parsing xml string in java?java解析xml字符串时出现异常如何解决?
【发布时间】:2015-07-16 15:25:42
【问题描述】:

我想解析 xml 字符串。这是我的java代码:

public Document loadXMLFromString(String xml) throws Exception
    {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        InputSource is = new InputSource(new StringReader(xml));
        return  builder.parse(is);
    }

但是在解析的时候,遇到了这个异常:

[Fatal Error] nitf-3-3.dtd:1:3: The markup declarations contained or pointed to by the document type declaration must be well-formed.

我无法弄清楚为什么会发生此异常。我认为它可能涉及“nitf”。有人可以帮帮我吗?

这是我的 xml 字符串:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE nitf SYSTEM "http://www.nitf.org/IPTC/NITF/3.3/specification/dtd/nitf-3-3.dtd">
<nitf change.date="June 10, 2005" change.time="19:30" version="-//IPTC//DTD NITF 3.3//EN">
  <head>
    <title>Dr. Nina M. Hill To Wed in April</title>
    <meta content="1" name="publication_day_of_month"/>
    <meta content="1" name="publication_month"/>
    <meta content="1989" name="publication_year"/>
    <meta content="Sunday" name="publication_day_of_week"/>
    <meta content="Society Desk" name="dsk"/>
    <meta content="39" name="print_page_number"/>
    <meta content="1" name="print_section"/>
    <meta content="1" name="print_column"/>
    <meta content="Style" name="online_sections"/>
    <docdata>
      <doc-id id-string="210943"/>
      <doc.copyright holder="The New York Times" year="1989"/>
      <identified-content>
        <classifier class="indexing_service" type="descriptor">WEDDINGS AND ENGAGEMENTS</classifier>
        <person class="indexing_service">HILL, NINA MARIE</person>
        <person class="indexing_service">FRANCIS, WALTER</person>
        <classifier class="online_producer" type="taxonomic_classifier">Top/Features/Style</classifier>
        <classifier class="online_producer" type="taxonomic_classifier">Top/Features/Style/Fashion and Style</classifier>
        <classifier class="online_producer" type="taxonomic_classifier">Top/Features/Style/Fashion and Style/Weddings and Celebrations</classifier>
        <classifier class="online_producer" type="general_descriptor">Weddings and Engagements</classifier>
      </identified-content>
    </docdata>
    <pubdata date.publication="19890101T000000" ex-ref="http://query.nytimes.com/gst/fullpage.html?res=950DEEDE1730F932A35752C0A96F948260" item-length="180" name="The New York Times" unit-of-measure="word"/>
  </head>
  <body>
    <body.head>
      <hedline>
        <hl1>Dr. Nina M. Hill To Wed in April</hl1>
      </hedline>
    </body.head>
    <body.content>
      <block class="lead_paragraph">
        <p>LEAD: The engagement of Dr. Nina Marie Hill to Walter Francis Raquet, the son of Mr. and Mrs. Peter Raquet of Tequesta, Fla., has been made known by her parents, Ilda Kleiner of Delray Beach, Fla., and Peter Hill of New York. The couple plan to marry in April.</p>
      </block>
      <block class="full_text">
        <p>LEAD: The engagement of Dr. Nina Marie Hill to Walter Francis Raquet, the son of Mr. and Mrs. Peter Raquet of Tequesta, Fla., has been made known by her parents, Ilda Kleiner of Delray Beach, Fla., and Peter Hill of New York. The couple plan to marry in April.</p>
        <p>Mr. Raquet is the senior vice president for marketing and new products at Herzog, Heine, Geduld, market makers for over-the-counter stocks and bonds in New York. He graduated from New York University. His father is a retired investor.</p>
      </block>
    </body.content>
  </body>
</nitf>

【问题讨论】:

标签: java xml


【解决方案1】:

我在使用你的 xml 时遇到了同样的错误,我刚刚删除了第二行并且它起作用了:

<!DOCTYPE nitf SYSTEM "http://www.nitf.org/IPTC/NITF/3.3/specification/dtd/nitf-3-3.dtd">

【讨论】:

【解决方案2】:

您可以解析xml,而无需删除DOCTYPE,使用自定义EntityResolver如下:

documentBuilder.setEntityResolver(new EntityResolver(){ 
    public InputSource resolveEntity(String publicId, String systemId){ 
        return new InputSource(new ByteArrayInputStream(new byte[0])); 
    } 
}); 

【讨论】:

    【解决方案3】:

    您在编写 dtd 时犯了一些错误,因此请查看下面给出的链接: The markup declarations contained or pointed to by the document type declaration must be well-formed

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-08-11
      • 2015-10-09
      • 2011-02-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多