hidemyname
hidemyname

Reputation: 4287

How to solve the exception when parsing xml string in java?

I want to parse the xml string. This is my java code:

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);
    }

But when parsing , I met this exception:

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

I can't figure out why this exception occurred. I think it may involve the "nitf". Can anybody help me?

This is my xml string:

<?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>

Upvotes: 1

Views: 1492

Answers (3)

Santhosh Kumar Tekuri
Santhosh Kumar Tekuri

Reputation: 3020

you can parse the xml, without removed DOCTYPE by using custom EntityResolver as below:

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

Upvotes: 1

Constantin
Constantin

Reputation: 1506

I got same error using your xml, i just deleted the second line and it worked:

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

Upvotes: 1

Piyush Mittal
Piyush Mittal

Reputation: 1890

you have done some mistakes while writing your dtd so just have a look on the link given below: The markup declarations contained or pointed to by the document type declaration must be well-formed

Upvotes: 0

Related Questions