Dom:document object Model (Document object models)
For XML application development, the DOM is an object-based XML data interface, which is a language-independent, platform-agnostic, standardized interface specification.
The DOM consists of three parts: core, Html,xml.
Strictly differentiate between the root node and root element node in the number of XML documents: the root node (document) represents the XML itself, which is the portal where we parse the XML document, and the root element node represents the root element of the XML document, which corresponds to the root of the document.
Four basic interfaces of the DOM Document,node,nodelist,namenodemap
JAXP (Java API for XML parsing): The Java API for XML parsing.
Documentbuilderfactory dbf = Documentbuilderfactory.newinstance ();
The purpose of using documentbuilderfactory is to create a program that is not related to a specific parser, when the static method Newinstance () of the Documentbuilderfactory class is called. He decides which parser to use based on a system variable, and because all parsers obey the interface defined by JAXP, the code is the same regardless of which parser is used, so when switching between different parsers, you only need to change the values of the system variables, not change any code. This is the benefit of the factory.
The relationship between Documentbuilderfactory and Documentbuilderfactory.
<?xml version= "1.0" encoding= "Utf-8"?>
< student roster xmlns:xsi= "http://www.w3.org/2001/XMLSchema-instance" xsi:nonamespaceschemalocation= "student.xsd" >
< student number = "1" >
< name > Zhang San </name >
< sex > male </sex >
< age >20</Age >
</Students >
< student number = "2" >
< name > John Doe </name >
< sex > female </sex >
< age >19</Age >
</Students >
< Student number = "3" >
< name > Harry </name >
< sex > male </sex >
< age >21</Age >
</Students >
</Student Roster >
Realization of the idea of recursion
public class DomTest3
{
public static void Main (string[] args) throws Parserconfigurationexception, Saxexception, IOException
{
Documentbuilderfactory dbf = Documentbuilderfactory.newinstance ();
Documentbuilder db = Dbf.newdocumentbuilder ();
Document doc = db.parse (new File ("Student.xml"));
Element root = Doc.getdocumentelement ();
Parseelement (root);
}
private static void Parseelement (element Element)
{
String tagName = Element.getnodename ();
NodeList children = Element.getchildnodes ();
System.out.print ("<" +tagname);
The Namenodemap object, which is composed of all the attributes of element elements, needs to be judged
NamedNodeMap map = Element.getattributes ();
If you have attributes
if (null!=map)
{
for (int i = 0;i<map.getlength (); i++)
{
Or the elements of each property
Attr Attr = (Attr) map.item (i);
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.