![]() |
The Java Developers Almanac 1.4Order this book from Amazon. |
e514. Converting CDATA Nodes into Text Nodes While Parsing an XML FileBy default, CDATA nodes in an XML file will be represented withCDATASection objects in a DOM document. This example demonstrates how
to create a parser that converts CDATA nodes into text nodes.
try { // Create a builder factory DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); // Configure it to coalesce CDATA nodes factory.setCoalescing(true); // Create the builder and parse the file Document doc = factory.newDocumentBuilder().parse(new File("infilename.xml")); // doc will not contain any CDATA 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"?> <root> Some pretext <![CDATA[Some text with lots of <"!@#$%'^&*()> special characters]]> Some posttext </root>and output: <?xml version="1.0" encoding="UTF-8"?> <root> Some pretext Some text with lots of <"!@#$%'^&*()> special characters Some posttext </root>
e516. Preventing Expansion of Entity References While Parsing an XML File
© 2002 Addison-Wesley. |