DOM Parsing XML generalization

Source: Internet
Author: User
Tags xpath
General parsing


XML File


<?xml version= ' 1.0 ' encoding= ' UTF-8 ' standalone= ' yes '?>
<logs>
	<mail>
		<username >san.zhang</username>
		<password>888888</password>
	</mail>
	<log>
		<project name= "First selection box option:" >project123</project>
		<category name= "The Second selection box option:" >category123 </category>
		<subcategory name= "Third selection box option: >subcategory123</subcategory>
		<time name=" Working time: ">10</time>
		<content name=" Work content: ">content123</content>
	</log>
	<log>
		<project name= "First selection box option: >project111</project>
		<category name=" Second selection box option: " >category111</category>
		<subcategory name= "Third selection box option:" >subcategory111</subcategory>
		<time name= "Working time:" >10</time>
		<content name= "Work content:" >content111</content>
	</log>
</logs>

Parsing Programs


/** * Document tree Parsing XML file * * @throws parserconfigurationexception * @throws ioexception * @throws saxexception * * public void test_001 () throws Parserconfigurationexception, Saxexception, IOException {documentbuilderfactory factor
		y = documentbuilderfactory.newinstance ();
		Documentbuilder builder = Factory.newdocumentbuilder ();
		File XML = new file (System.getproperty ("User.dir") + File.separator + "Log.xml");
		Create Document Object Document DOC = Builder.parse (XML);
		Get root node Element root = Doc.getdocumentelement ();
		SYSTEM.OUT.PRINTLN ("Root node Name:" + root.getnodename ());
		System.out.println ("BaseUri:" + Root.getbaseuri ());
		System.out.println ("LocalName:" + root.getlocalname ());
		System.out.println ("NamespaceURI:" + Root.getnamespaceuri ());
		System.out.println ("NodeType:" + root.getnodetype ());
		System.out.println ("TagName:" + root.gettagname ());
		System.out.println ("SchemaType:" + root.getschematypeinfo ()); System.out.println ("Textcontent: "+ root.gettextcontent ());
		Children nodes of root node nodelist nodes = Root.getchildnodes ();
		Children nodes Size System.out.println ("Children nodes Size:" + nodes.getlength ());
			for (int i = 0; i < nodes.getlength (); i++) {Node node = Nodes.item (i);
			System.out.println ("Item:" + node); if (!) (
			Node instanceof Element)) continue;
			element element = (element) node;
			System.out.println ("element:" + element.gettagname ()); for (Node child = Element.getfirstchild (), child!= null, child = child. Getnextsibling ()) {if (!) (
				Child instanceof Element)) continue;
				System.out.println (Child.getnodename () + ":" + child.getfirstchild (). Gettextcontent ());
			System.out.println (Child.getnodename () + ":" + child.getfirstchild (). Getnodevalue ()); }
		}
	}

DTD Validation parsing


XML File


<?xml version= ' 1.0 ' encoding= ' UTF-8 ' standalone= ' yes '?>
<! DOCTYPE logs SYSTEM "CONFIG.DTD" >
<logs>
	<mail>
		<username>san.zhang</username >
		<password>888888</password>
	</mail>
	<log>
		<project name= " First selection box option: ">project123</project>
		<category name=" The Second selection box option: ">category123</category>
		<subcategory name= "Third selection box option:" >subcategory123</subcategory>
		<time name= "Working time:" >10 </time>
		<content name= "Work content:" >content123</content>
	</log>
</logs>

Which joined the <! DOCTYPE logs SYSTEM "CONFIG.DTD" >


CONFIG.DTD


<?xml version= "1.0" encoding= "UTF-8"?> <!
ELEMENT logs (Mail,log) >
<! ELEMENT Mail (Useraname,password) >
<! ELEMENT log (project,category,subcategory,time,content) >
<! ELEMENT username (#PCDATA) >
<! ELEMENT password (#PCDATA) >
<! ELEMENT Project (#PCDATA) >
<! ELEMENT category (#PCDATA) >
<! ELEMENT subcategory (#PCDATA) >
<! ELEMENT time (#PCDATA) >
<! ELEMENT content (#PCDATA) >


Parsing Programs


/** * Document tree with DTD validation parse XML file * @throws parserconfigurationexception * @throws ioexception * @throws saxexception * *  public void test_002 () throws Parserconfigurationexception, Saxexception, IOException {documentbuilderfactory factory
		= Documentbuilderfactory.newinstance ();
		Open Validate feature factory.setvalidating (true);
		Ignore whitespace factory.setignoringelementcontentwhitespace (true);
		Documentbuilder builder = Factory.newdocumentbuilder ();
		Builder.seterrorhandler (New MyErrorHandler ());
		File XML = new file (System.getproperty ("User.dir") + File.separator + "Log_dtd.xml");
		Create Document Object Document DOC = Builder.parse (XML);
		Get root node Element root = Doc.getdocumentelement ();
		
		NodeList nodes = Root.getchildnodes ();
		Element mail = (element) nodes.item (0);
		Element log = (element) Nodes.item (1);
		System.out.println (Mail.getnodename ());
		System.out.println (Log.getnodename ()); System.out.println ("content:" + log.getchildNodes (). Item (4). Gettextcontent ()); }

Schema parsing xml


XML File


<?xml version= ' 1.0 ' encoding= ' UTF-8 ' standalone= ' yes '?> <logs ' xmlns:xsi=
Xmlschema-instance "
    xsi:nonamespaceschemalocation=" config.xsd ">
	<mail>
		<username> san.zhang</username>
		<password>888888</password>
	</mail>
	<log>
		<project name= "First selection box option:" >project123</project>
		<category name= "Second selection box option:" >category123< /category>
		<subcategory Name= "The Third selection box option:" >subcategory123</subcategory>
		<time name= " Working time: ">10</time>
		<content name=" Work content: ">content123</content>
	</log>
</logs>

schema file


<?xml version= "1.0" encoding= "UTF-8"?> <xsd:element name= "Useraname"
, type= "xsd:string" >
< Xsd:element name= "Password", type= "xsd:string" >
<xsd:element name= "project", type= "xsd:string" >
<xsd:element name= "category", type= "xsd:string" > <xsd:element name= "subcategory"
, type= "xsd:string" >
<xsd:element name= "Time", type= "Xsd:int" >
<xsd:element name= "content", type= "xsd:string" >

Parsing Programs

/** * Document tree Parsing XML with schema validation * @throws parserconfigurationexception * @throws IOException * @throws saxexception */public void test_003 () throws Parserconfigurationexception, Saxexception, ioexception{documentbuilderfactory
		y = documentbuilderfactory.newinstance ();
		Factory.setnamespaceaware (TRUE);
		Final String jaxp_schema_language = "Http://java.sun.com/xml/jaxp/properties/schemaLanguage";
		Final String W3c_xml_schema = "Http://www.w3.org/2001/XMLSchema";
		Factory.setattribute (Jaxp_schema_language, W3c_xml_schema);
		Documentbuilder builder = Factory.newdocumentbuilder ();
		Builder.seterrorhandler (New MyErrorHandler ());
		File XML = new file (System.getproperty ("User.dir") + File.separator + "Log_schema.xml");
		Create Document Object Document DOC = Builder.parse (XML);
		Get root node Element root = Doc.getdocumentelement ();
		NodeList nodes = Root.getchildnodes ();
		Element log = (element) Nodes.item (1);
		System.out.println (Log.getnodename ());System.out.println ("content:" + log.getchildnodes (). Item (4). Gettextcontent ()); }

XPath


XML file

<?xml version= ' 1.0 ' encoding= ' UTF-8 ' standalone= ' yes '?>
<logs>
	<mail>
		<username >san.zhang</username>
		<password>888888</password>
	</mail>
	<log>
		<project name= "First selection box option:" >project123</project>
		<category name= "The Second selection box option:" >category123 </category>
		<subcategory name= "Third selection box option: >subcategory123</subcategory>
		<time name=" Working time: ">10</time>
		<content name=" Work content: ">content123</content>
	</log>
	<log>
		<project name= "First selection box option: >project111</project>
		<category name=" Second selection box option: " >category111</category>
		<subcategory name= "Third selection box option:" >subcategory111</subcategory>
		<time name= "Working time:" >10</time>
		<content name= "Work content:" >content111</content>
	</log>
</logs>

Parsing Programs


/** * XPath reads XML * @throws parserconfigurationexception * @throws ioexception * @throws saxexception * @throw S xpathexpressionexception */public void test_004 () throws Parserconfigurationexception, Saxexception, IOException, X
		pathexpressionexception{xpathfactory xfactory = Xpathfactory.newinstance ();
		Documentbuilderfactory factory = Documentbuilderfactory.newinstance ();
		Documentbuilder builder = Factory.newdocumentbuilder ();
		File XML = new file (System.getproperty ("User.dir") + File.separator + "Log.xml");
		Create Document Object XPath Path = Xfactory.newxpath ();
		Document doc = Builder.parse (XML);
		String username = path.evaluate ("/logs/mail/username", Doc);
		SYSTEM.OUT.PRINTLN (username);
		NodeList nodes = (nodelist) path.evaluate ("/logs/log", Doc,xpathconstants.nodeset);
		System.out.println (Nodes.item (1). Getfirstchild (). Getnodename ());
		Node node = (node) path.evaluate ("logs/log[1]", Doc, Xpathconstants.node); System.out.println (node.getfirsTchild (). Getnodename ());
		int count = ((number) path.evaluate ("/logs/log/time", Doc, Xpathconstants.number)). Intvalue ();
	System.out.println (count); }


Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.