XML parser points

xiaoxiao2021-03-06  59

1, all elements must be properly closed, the empty element is handled

2, the label must not be cross

3, all attributes have to be packaged

4, element, attribute name case case

5, CDATA area

Words to write

second line

]]]]

Parser setting

One of the advantages of creating a parser using the DocumentBuilder is to control various settings on the parser created by DocumentBuilderFactory. For example, you can set the parser verification document:

...

Try {

DocumentBuilderFactory DBF = DocumentBuilderFactory.newinstance ();

DBF.SetValidating (TRUE);

DocumentBuilder DB = dbf.newdocumentbuilder ();

DOC = db.parse (docfile);

} catch (exception e) {

...

Java's DOM Level 2 implementation allows the parser to be controlled by the following method:

Setcoalescing (): Decides whether the parser is to convert the CDATA node into text, and if you want to merge the surrounding text node (if applicable). Its default is false.

setExpandentityReference (): Determine if you want to expand external entity references. If true, the external data will be inserted into the document. Its default is TRUESETIGNORINGCOMMENTS (): Determine if you want to ignore the comments in the file. Its default is false.

SetignoringeElementContentWhitespace (): Determine if you want to ignore blank in element content (similar to the way of the browser to treat HTML). Its default is false.

SetNameSpaceaware (): Determine if the parser should pay attention to the namespace information. Its default is false.

SetValidating (): By default, the parser does not verify the document. Set this parameter to TRUE to open the verification function.

Parser abnormal

Many places may be wrong due to all these possibilities in the creation parser. As the example here, the application dumps all of these contents into a single universal Exception, which may not be very helpful in terms of debugging.

To better identify problems, you can capture specific exceptions associated with all aspects of the parser:

...

Try {

DocumentBuilderFactory DBF = DocumentBuilderFactory.newinstance ();

DocumentBuilder DB = dbf.newdocumentbuilder ();

DOC = db.parse (docfile);

} catch (javax.xml.parsers.ParserConfigurationException PCE) {

System.out.println ("The Parser Was Not Configured Correctly);

System.exit (1);

} catch (java.io.ioException IE {

System.out.println ("Cannot Read INPUT FILE.");

System.exit (1);

} catch (org.xml.sax.saxexception se) {

System.out.println ("Problem Parsing The File.");

System.exit (1);

} catch (java.lang.illegalargumentexception ae) {system.out.println ("" please specify an xml source. ");

System.exit (1);

}

...

Once the parser has created a Document, the application can write it to debug it to check the data.

转载请注明原文地址:https://www.9cbs.com/read-113129.html

New Post(0)