Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/13.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
如何解决在java中解析xml字符串时出现的异常?_Java_Xml - Fatal编程技术网

如何解决在java中解析xml字符串时出现的异常?

如何解决在java中解析xml字符串时出现的异常?,java,xml,Java,Xml,我想解析xml字符串。这是我的java代码: public Document loadXMLFromString(String xml) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); InputSo

我想解析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>

尼娜·M·希尔博士将于4月结婚
婚约
希尔,尼娜·玛丽
弗朗西斯,沃尔特
上衣/特点/风格
上衣/特点/风格/时尚和风格
顶级/特色/风格/时尚和风格/婚礼和庆典
婚约
尼娜·M·希尔博士将于4月结婚
线索:妮娜·玛丽·希尔博士与沃尔特·弗朗西斯·拉奎特订婚,沃尔特·弗朗西斯·拉奎特是佛罗里达州特克斯塔的彼得·拉奎特夫妇的儿子,她的父母、佛罗里达州德雷海滩的伊尔达·克莱纳和纽约的彼得·希尔都知道了这一点。这对夫妇计划在四月份结婚

线索:妮娜·玛丽·希尔博士与沃尔特·弗朗西斯·拉奎特订婚,沃尔特·弗朗西斯·拉奎特是佛罗里达州特克斯塔的彼得·拉奎特夫妇的儿子,她的父母、佛罗里达州德雷海滩的伊尔达·克莱纳和纽约的彼得·希尔都知道了这一点。这对夫妇计划在四月份结婚

Raquet先生是Herzog、Heine、Geduld的市场营销和新产品高级副总裁,该公司是纽约场外股票和债券的做市商。他毕业于纽约大学。他的父亲是一位退休的投资者


您在编写dtd时犯了一些错误,请查看以下链接:

我使用您的xml时也遇到了同样的错误,我刚刚删除了第二行,它成功了:

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

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

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

似乎
XML
不符合指定的
DTD
。我认为您的XML文件格式不正确,请特别注意,该URI无效。它重定向到显然不是有效DTD的位置。