DOM (learning) Java one

xiaoxiao2021-03-06  73

In order to use information in the XML file, the file must be parsed to create a Document object. The Document object is an interface, so it cannot be instantiated directly; in general, the application uses a factory accordingly. The accurate process varies from realization, but basic thinking is the same. (Similarly, Level 3 standardizes this task.) In this example Java environment, the parsing file is a three-step process: create DocumentBuilderFactory. DocumentBuilderFactory object creates DocumentBuilder. Create DocumentBuilder. DocumentBuilder performs actual resolution to create a Document object. Analyze the file to create a Document object. Now you can start building an app. Such as: import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.DocumentBuilder; import org.w3c.dom.DOMImplementation; import org.w3c.dom *; import java.io *; public class ShowDomImpl {public.. Static void main (string args []) {Try {file file = new file ("Order.xml"); Document Doc = NULL; DocumentBuilderFactory DBF =

DocumentBuilderFactory.newInstance (); DocumentBuilder docb = dbf.newDocumentBuilder (); doc = docb.parse (file);} catch (Exception e) {}}} is obtained for the root node doc = docb.parse (file); Element root = Doc.getdocumentelement (); system.out.println ("The root of the xml document is

" root.getnodename ()); get a list of root nodes: nodelist children = root.getchildNodes (); system.out.println (" there is " children.getLength () " node. "); use getFirstChild () And getnextsibling () for (Node Child = root.getfirstchild (); child!

= NULL; Child = root.getnextsibling ()) Implementing a function of recursive definition in multi-storey children PUBLIC VOID Stepth (Node Start) {system.out.println (Start.Getnodename ()

"=" start.getnodevalue ()); for (node ​​child = start.getfirstchild (); child!

= null; child = start.getnextsibling ()) {stepthrough (child);} Call setthrough (root) in the main function

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

New Post(0)