XML file: DomParse. xml
<? Xml version = "1.0" encoding = "gb2312"?> <Root> <node> tagged text </node> <! [CDATA [character reference: & #41; & #62; & #63;]> corresponding character: & #41; & #62; & #63; </node> </root>
Java file: DomParse. java
Import org. w3c. dom. *; import javax. xml. parsers. *; import java. io. *; public class DomParse {public DomParse () {try {// obtain the DocumentBuilderFactory object DocumentBuilderFactory domFac = DocumentBuilderFactory. newInstance (); // create the parser DocumentBuilder documentbuilder = domFac. newDocumentBuilder (); // parse the specified Document document = documentbuilder. parse (new File ("textData. xml "); // obtain the node tag list NodeList nodelist = document based on the tag name. getElementsByTagName ("node"); for (int I = 0; I <nodelist. getLength (); I ++) {Node node = nodelist. item (I); String content = node. getTextContent (); System. out. println (content) ;}} catch (Exception e) {e. printStackTrace () ;}} public static void main (String [] args) {new DomParse ();}}
This article is from the "Aegean Sea" blog, please be sure to keep this source http://piano.blog.51cto.com/3954765/1298238