2010-11-04 13 views
7

Viene visualizzato il seguente errore quando si tenta di eseguire il mio programma java (è necessario leggere un file xml e stampare parte del contenuto).Problema entità elaborazione XML Java?

Da quello che ho capito c'è un'entità non referenziata che non fa parte dello standard xml quindi la mia domanda è; come posso risolvere questo problema?

Grazie,

[Fatal Error] subject.xml:4:233: The entity "rsquo" was referenced, but not declared. 
org.xml.sax.SAXParseException: The entity "rsquo" was referenced, but not declared. 
at com.sun.org.apache.xerces.internal.parsers.DOMParser.parse(Unknown Source) 
at com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderImpl.parse(Unknown Source) 
at javax.xml.parsers.DocumentBuilder.parse(Unknown Source) 
at DomParserExample2.parseXmlFile(DomParserExample2.java:42) 
at DomParserExample2.runExample(DomParserExample2.java:24) 
at DomParserExample2.main(DomParserExample2.java:115) 
Exception in thread "main" java.lang.NullPointerException 
at DomParserExample2.parseDocument(DomParserExample2.java:54) 
at DomParserExample2.runExample(DomParserExample2.java:27) 
at DomParserExample2.main(DomParserExample2.java:115) 

risposta

6

L'entità ’ non è un XML-entità. È definito in HTML: http://en.wikipedia.org/wiki/List_of_XML_and_HTML_character_entity_references

Se è stato creato l'XML è possibile aggiungere Entità alla DTD.

Qualcosa come questo potrebbe aiutare: http://gv.ca/dtd/character-entities.dtd

edit: Per risolvere questo problema è possibile aggiungere un DTD al file XML (se non è già definito).

tuo XML:

<?xml version="1.0" encoding="ISO-8859-1" ?> 
<!DOCTYPE demo SYSTEM "./demo.dtd"> 
<demo> 
    &rsquo; 
</demo> 

tuo DTD:

<!ELEMENT demo (#PCDATA)> 
<!ENTITY rsquo "&#8217;"> 

Se si fornisce la DTD per l'applicazione, l'errore va via. Non scriverei alle Entites da solo, ne utilizzerei uno da W3C http://www.w3.org/TR/xhtml1/DTD/xhtml-lat1.ent

Come includere la DTD per il tuo XML è un'altra domanda. Per quanto mi ricordo, puoi impostare il percorso per la DTD o un file di catalogo.

Edit 2: Date un'occhiata al EntityResolver: http://download.oracle.com/javase/1.4.2/docs/api/org/xml/sax/EntityResolver.html

+0

così dovrei aggiungere la riga al mio documento xml e i miei problemi andranno via? –

+1

Ho aggiunto qualche altra informazione alla mia risposta –

0
/** 
     * This Inner class is written to solve the XML parsing DTD validation 
     * checking from online because if Internet is not connected, then it 
     * throws Exception. 
     * 
     * @author Ravi Thapa 
     */ 




public class CustomEntityResolver implements EntityResolver 
    { 
     public InputSource resolveEntity(String publicId, String systemId) 
     { 
      InputSource source = null; 
      Pattern pattern1 = 
        Pattern.compile("^-//(.*)//DTD(.*)$", Pattern.CASE_INSENSITIVE); 
      Matcher match1 = pattern1.matcher(publicId.trim()); 

      Pattern pattern2 = 
        Pattern.compile("^http://(.*).dtd$", Pattern.CASE_INSENSITIVE); 
      Matcher match2 = pattern2.matcher(systemId.trim()); 
      if (match1.find() || match2.find()) 
      { 
       source = new InputSource(new ByteArrayInputStream("".getBytes())); 
      } 

      // return null to signal default behavior 
      return source; 
     } 
    } 
3

seguito la risposta di Christian, hai anche la possibilità di dichiarare le entità a destra in XML

<!DOCTYPE your_type [ 
    <!ENTITY rsquo "&#8217;"> 
    <!ENTITY lsquo "&#8216;"> 
]>