码迷,mamicode.com
首页 > 编程语言 > 详细

javaee之xml和dtd的基础练习

时间:2016-07-13 16:47:25      阅读:267      评论:0      收藏:0      [点我收藏+]

标签:

从昨天的学习中,我学习到了xml的基础知识,包括dtd的约束基础

一、首先 是关于dtd的联系,关于这部分我们知识需要了解能够根据dtd约束来写出xml文件即可

1)首先是dtd文件:

<!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)然后根据dtd写出xml

TVSCHEDULE  :是根标签 里面有一个子元素 <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 :这个元素有 <span style="font-family: Arial, Helvetica, sans-serif;">BANNER,DAY这两个子元素</span>
<span style="font-family: Arial, Helvetica, sans-serif;"></span><pre name="code" class="html"><span style="font-family: Arial, Helvetica, sans-serif;">BANNER  :这个元素可以随便写字符串,因为 #PCDATA</span>
<span style="font-family: Arial, Helvetica, sans-serif;">DAY :这个元素中有 </span><span style="font-family: Arial, Helvetica, sans-serif;">DATE,HOLIDAY|PROGRAMSLOT 这几个子元素</span>
<span style="font-family: Arial, Helvetica, sans-serif;">HOLIDAY 和 DATE : 这两个元素都可以随便写字符串 </span>
<span style="font-family: Arial, Helvetica, sans-serif;"></span><pre name="code" class="html"><span style="font-family: Arial, Helvetica, sans-serif;">PROGRAMSLOT  :这里面有</span><span style="font-family: Arial, Helvetica, sans-serif;">TIME,TITLE,DESCRIPTION这几个子元素,也是可以写字符串</span>
<span style="font-family: Arial, Helvetica, sans-serif;"></span><pre name="code" class="html">TVSCHEDULE 的 NAME 属性 必须有,因为#REQUIRED<pre name="code" class="html">CHANNEL 的 CHAN 属性也是必须有<pre name="code" class="html">PROGRAMSLOT 的 VTR 属性、 <span style="font-family: Arial, Helvetica, sans-serif;">TITLE  的 RATING  属性 、  </span><span style="font-family: Arial, Helvetica, sans-serif;">TITLE 的 LANGUAGE  属性 是可选的,不一定必须有,因为#IMPLIED</span>
<span style="font-family: Arial, Helvetica, sans-serif;">
</span>
<span style="font-family: Arial, Helvetica, sans-serif;">3)、写出来之后便是</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></HOLIDAY>
			<PROGRAMSLOT VTR="abc">
				<TIME>abc</TIME>
				<TITLE LANGUAGE="abc" RATING="abc">abc</TITLE>
				<DESCRIPTION>abc</DESCRIPTION>
			</PROGRAMSLOT>
		</DAY>
		
	</CHANNEL>
	
	
</TVSCHEDULE>










二、 今天还学习到有关xml读取加载的一些基本操作

1)首先有一个xml文件

<?xml version="1.0" encoding="UTF-8" ?>
<Location>
    <State Name="北京" Code="11">
      <City Name="东城" Code="1" />
      <City Name="西城" Code="2" />
      <City Name="朝阳" Code="5" />
      <City Name="丰台" Code="6" />
      <City Name="石景山" Code="7" />
      <City Name="海淀" Code="8" />
</State>
    <State Name="天津" Code="12">
      <City Name="和平" Code="1" />
      <City Name="河东" Code="2" />
      <City Name="河西" Code="3" />
      <City Name="南开" Code="4" />
</State>
    <State Name="河北" Code="13">
      <City Name="石家庄" Code="1">
        <Region Name="长安区" Code="2" />
        <Region Name="桥东区" Code="3" />
        <Region Name="桥西区" Code="4" />
        <Region Name="新华区" Code="5" />
      </City>
 </State>
</Location>

2)、编写一个程序是用来加载xml文件的

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
		//查找xml,首先创建DocumentBuilderFactory实例
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		//通过DocumentBuilderFactory实例来创建DocumentBuilder实例
		DocumentBuilder builder = factory.newDocumentBuilder();
		//通过DocumentBuilder的实例调用parse的方法来读取xml文件
		Document doc = builder.parse("src/LocList.xml");

           test04(doc);
	}

//1、遍历所有的节点
	public static void test01(Document doc){
		Node item = doc.getElementsByTagName("Location").item(0);
		
		//调用子节点的递归函数
		getChilderNode(item);
	}


	private static void getChilderNode(Node node) {
		// TODO Auto-generated method stub
		//先得到当前的节点如果节点的类型是元素,则打印
		if(node.getNodeType() == Node.ELEMENT_NODE){
			System.out.println(node.getNodeName());
		}
		//再得到这个元素下的子元素遍历之,在递归
		NodeList nodeList = node.getChildNodes();
		for(int i=0;i<nodeList.getLength();i++){
			Node item = nodeList.item(i);
			getChilderNode(item);
		}
	}
	
	//2、查询某一个节点
		//2.1找寻名字为“东城”的节点和其他的属性
	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());
		//得到了名为“东城”这个city下的Name属性和Code属性
		System.out.println(celement.getAttribute("Name"));
		System.out.println(celement.getAttribute("Code"));
	
		//得到City里面的文本内容
		int index = selement.getElementsByTagName("City").getLength();
		Element c = (Element) selement.getElementsByTagName("City").item(index-1);
		
		System.out.println(c.getTextContent());
	}
	
	
	//3、修改某一个节点
		//3.1添加一个State
	public static void test03(Document doc) throws Exception{
		Element element = doc.createElement("State");
		
		//3.2在state元素中添加属性Name和Code
		element.setAttribute("Name", "广东");
		element.setAttribute("Code", "120");
		
		//3.3在state中添加City和其他属性
		Element celement = doc.createElement("City");
		celement.setAttribute("Name","翁源");
		celement.setAttribute("Code", "515");
		celement.setTextContent("龙仙");
		
		element.appendChild(celement);
		
		Node root = doc.getElementsByTagName("Location").item(0);
		root.appendChild(element);
		
		//1、通过TransformerFactory来创建实例,调用newInstance方法
		TransformerFactory transformerFactory = TransformerFactory.newInstance();
		//2、通过TransformerFactory的实例来创建newTransformer对象
		Transformer transformer = transformerFactory.newTransformer();
		//3、用Transformer的对象的transform方法来修改xml文件
		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、通过TransformerFactory来创建实例,调用newInstance方法
		TransformerFactory transformerFactory = TransformerFactory.newInstance();
		//2、通过TransformerFactory的实例来创建newTransformer对象
		Transformer transformer = transformerFactory.newTransformer();
		//3、用Transformer的对象的transform方法来修改xml文件
		transformer.transform(new DOMSource(),new StreamResult("src/LocList.xml"));
				
	}

}


通过这几个案例我能够基本了解到了,关于xml和dtd 的相关知识

javaee之xml和dtd的基础练习

标签:

原文地址:http://blog.csdn.net/css1223mjf/article/details/51891774

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!