这个压缩包里的都是超级经典的java例子
源代码在线查看: ignorecomments.htm
Ignoring Comments While Parsing an XML File (Java Developers Almanac Example)
The Java Developers Almanac 1.4
Order this book from Amazon.
google_ad_client = "pub-6001183370374757";
google_ad_width = 120;
google_ad_height = 600;
google_ad_format = "120x600_as";
google_ad_channel = "4777242811";
google_ad_type = "text_image";
google_color_border = "FFFFFF";
google_color_bg = "FFFFFF";
google_color_link = "6666CC";
google_color_url = "6666CC";
google_color_text = "000000";
//-->
Home
>
List of Packages
>
javax.xml.parsers
[8 examples]
>
DOM Parsing Options
[3 examples]
e515. Ignoring Comments While Parsing an XML File
By default, Comment nodes are created for each comment in an XML file.
If it is not necessary to preserve the comments, there is no need to
create the nodes. This example demonstrates how to create a parser
that ignores comments.
try {
// Create a builder factory
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
// Configure it to ignore comments
factory.setIgnoringComments(true);
// Create the builder and parse the file
Document doc = factory.newDocumentBuilder().parse(new File("infilename.xml"));
// doc will not contain any Comment nodes
} catch (SAXException e) {
// A parsing error occurred; the xml input is not valid
} catch (ParserConfigurationException e) {
} catch (IOException e) {
}
Here's some sample input:
<?xml version="1.0" encoding="UTF-8"?>
<!-- comment -->
<root>
Some text
<!-- comment -->
Some text
</root>
<!-- comment -->
and output:
<?xml version="1.0" encoding="UTF-8"?>
<root>
Some text
Some text
</root>
Related Examples
e514.
Converting CDATA Nodes into Text Nodes While Parsing an XML File
e516.
Preventing Expansion of Entity References While Parsing an XML File
See also:
DOM
SAX
© 2002 Addison-Wesley.