Basic practice of XML and DTD for Java EE

Source: Internet
Author: User
Tags cdata

From yesterday's study, I learned the basic knowledge of XML, including the constraints of the DTD base

First, the link to the DTD, about which we need to know to be able to write XML files based on DTD constraints

1) First, the DTD file:

<! ELEMENT tvschedule (channel+) ><! ELEMENT CHANNEL (banner,day+) ><! ELEMENT BANNER (#PCDATA) ><! ELEMENT Day (DATE, (holiday| programslot+) +) ><! ELEMENT HOLIDAY (#PCDATA) ><! ELEMENT DATE (#PCDATA) ><! ELEMENT Programslot (time,title,description?) ><! ELEMENT time (#PCDATA) ><! ELEMENT TITLE (#PCDATA) > <! ELEMENT DESCRIPTION (#PCDATA) ><! Attlist tvschedule NAME CDATA #REQUIRED ><! Attlist CHANNEL CHAN CDATA #REQUIRED ><! Attlist programslot VTR CDATA #IMPLIED ><! Attlist TITLE RATING CDATA #IMPLIED ><! Attlist TITLE LANGUAGE CDATA #IMPLIED >

2) then write the XML according to the DTD

Tvschedule  : There is a child element inside the root tag <span style= "Font-family:arial, Helvetica, Sans-serif;" >channel </span>
<span style= "font-family:arial, Helvetica, Sans-serif;" ></span><pre name= "code" class= "HTML" >channel: This element has <span style= "font-family:arial, Helvetica, Sans-serif; " >banner,day these two sub-elements </span>
<span style= "font-family:arial, Helvetica, Sans-serif;" ></span><pre name= "code" class= "HTML" ><span style= "font-family:arial, Helvetica, Sans-serif;" >banner  : This element can be arbitrarily written as a string because #PCDATA </span>
<span style= "font-family:arial, Helvetica, Sans-serif;" >day: This element has </span><span style= "font-family:arial, Helvetica, Sans-serif;" >date,holiday| Programslot these sub-elements </span>
<span style= "font-family:arial, Helvetica, Sans-serif;" >holiday and DATE: Both elements can be arbitrarily written in strings </span>
<span style= "font-family:arial, Helvetica, Sans-serif;" ></span><pre name= "code" class= "HTML" ><span style= "font-family:arial, Helvetica, Sans-serif;" >programslot  : There are </span><span style= "font-family:arial, Helvetica, Sans-serif;" >time,title,description these few child elements, also can write string </span>
<span style= "font-family:arial, Helvetica, Sans-serif;" ></span><pre name= The Name property of the "code" class= "HTML" >tvschedule must have, because #required<pre name= "code" class= " The >channel CHAN attribute of HTML is also required to have <pre name= "code" class= "HTML" >programslot the VTR attribute, <span style= "Font-family:ari Al, Helvetica, Sans-serif; >title  RATING  attribute,  </span><span style= "font-family:arial, Helvetica, Sans-serif;" The LANGUAGE property of the >title  is optional and must not necessarily have, because #implied</span>
<span style= "font-family:arial, Helvetica, Sans-serif;" ></span>
<span style= "font-family:arial, Helvetica, Sans-serif;" >3), after writing, is </span>
<span style= "font-family:arial, Helvetica, Sans-serif;" ></span><pre name= "code" class= "HTML" ><?xml version= "1.0" encoding= "Utf-8"? ><! DOCTYPE tvschedule SYSTEM "xml_example.dtd" ><tvschedule name= "ACB" ><channel chan= "abc" ><banner >abc</BANNER><DAY><DATE>abc</DATE><HOLIDAY>abc</HOLIDAY><HOLIDAY> 



Second, today also learned some basic operations on XML read loading

1) First there is an XML file

<?xml version= "1.0" encoding= "UTF-8"?><location> <state    name= "Beijing" code= "one" >      <city Name= "Dongcheng" code= "1"/>      <city name= "Xicheng" code= "2"/>      <city name= "Chaoyang" code= "5"/>      <city name= "Fengtai" code= "6"/>      <city name= "Shijingshan" code= "7"/>      <city name= "Haidian" code= "8"/></state>    <state name= "Tianjin" code= ">      <city name=" Peace "code=" 1 "/>      <city name=" Hedong "code=" 2 "/>      <city name= "Hexi" code= "3"/>      <city name= "Nankai" code= "4"/></state>    <state name= "Hebei" code= " ">      <city name=" Shijiazhuang "code=" 1 ">        <region name=" Changan District "code=" 2 "/>        <region name=" Qiaodong District " Code= "3"/>        <region name= "Qiaoxi District" code= "4"/>        <region name= "Xinhua District" code= "5"/>      </City> </State></Location>

2), write a program is used to load the XML file

Import Javax.xml.parsers.documentbuilder;import Javax.xml.parsers.documentbuilderfactory;import Javax.xml.parsers.parserconfigurationexception;import Javax.xml.transform.transformer;import Javax.xml.transform.transformerconfigurationexception;import Javax.xml.transform.transformerfactory;import Javax.xml.transform.dom.domsource;import Javax.xml.transform.stream.streamresult;import org.w3c.dom.Document; Import Org.w3c.dom.element;import Org.w3c.dom.node;import Org.w3c.dom.nodelist;public class Xml_example1 {/** * @param args * @throws Exception */public static void Main (string[] args) throws Exception {//TODO auto-generated method stub//Check Find XML, first create Documentbuilderfactory instance documentbuilderfactory factory = Documentbuilderfactory.newinstance ();// Create an Documentbuilder instance by documentbuilderfactory instance Documentbuilder Builder = Factory.newdocumentbuilder ();//           The parse method is called through the instance of Documentbuilder to read the XML file document doc = Builder.parse ("Src/loclist.xml"); test04 (DOC);} 1. Traverse all nodes public static void test01(Document Doc) {Node item = doc.getelementsbytagname ("location"). Item (0);//Call the child node's recursive function Getchildernode (item);} private static void Getchildernode (node node) {//TODO auto-generated method stub//first gets the current node if the type of the node is an element, print if ( Node.getnodetype () = = Node.element_node) {System.out.println (Node.getnodename ());} The sub-elements under this element are then traversed, in the recursive nodeList nodeList = Node.getchildnodes (); for (int i=0;i<nodelist.getlength (); i++) {Node item = Nodelist.item (i); Getchildernode (item);}} 2. Query a node//2.1 search for a node named "Dongcheng" and other properties public static void test02 (Document doc) {element selement = (Element) Doc.getelementsbytagname ("state"). Item (0); Element celement = (Element) selement.getelementsbytagname ("City"). Item (0);//system.out.println ( Celement.gettextcontent ());//Get the name attribute and the code attribute System.out.println (Celement.getattribute ("name") under the city named "Dongcheng"; System.out.println (Celement.getattribute ("Code"));//Get the text content of city inside int index = selement.getelementsbytagname ("City" ). GetLength (); Element c = (Element) selement.getelementsbytagname ("City"). Item (index-1); System.out.println (C.gettextcontent ());} 3. Modify a node//3.1 add a statepublic static void test03 (Document doc) throws Exception{element Element = Doc.createelement (" State ");//3.2 add the attribute name and Codeelement.setattribute (" name "," Guangdong ") to the State element, Element.setattribute (" Code "," 120 ");// 3.3 Add city and other attributes in state to element celement = Doc.createelement ("City"), Celement.setattribute ("Name", "Wengyuan"); Celement.setattribute ("Code", "515"), Celement.settextcontent ("Longxian"); Element.appendchild (celement); Node root = Doc.getelementsbytagname ("location"). Item (0); Root.appendchild (Element),//1, transformerfactory to create an instance, Call the Newinstance method transformerfactory transformerfactory = Transformerfactory.newinstance ();//2, Create a Newtransformer object transformer transformer = Transformerfactory.newtransformer () through an instance of Transformerfactory,//3, Use the transform method of the transformer object to modify the XML file Transformer.transform (new Domsource (), New Streamresult ("Src/loclist.xml"));} public static void test04 (Document doc) throws Exception{element selement = (Element) Doc.getelementsbytagnaMe ("state"). Item (1); Element celement = (Element) selement.getelementsbytagname ("City"). Item (6); Celement.removeattribute ("Name"); Element celement1 = (Element) selement.getelementsbytagname ("City"). Item (7); Selement.removechild (celement1);//1, Create an instance from Transformerfactory, call the Newinstance method transformerfactory transformerfactory = Transformerfactory.newinstance () ;///2, create Newtransformer object Transformer transformer = Transformerfactory.newtransformer () by Transformerfactory instance,//3, Use the transform method of the transformer object to modify the XML file Transformer.transform (new Domsource (), New Streamresult ("Src/loclist.xml"));}}


In these cases I have a basic understanding of XML and DTD related knowledge

Basic practice of XML and DTD for Java EE

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.