day08 Xml与Tomcat

DTD约束demo

bookstore1.xml

<?xmlversion="1.0"encoding="UTF-8"?>
<!DOCTYPEbookstore[

<!ELEMENTbookstore(book+)>
<!ELEMENTbook(title,author,year,price)>
<!ELEMENTtitle(#PCDATA)>
<!ELEMENTauthor(#PCDATA)>
<!ELEMENTyear(#PCDATA)>
<!ELEMENTprice(#PCDATA)>

<!ATTLISTbookcategoryCDATA#required>
<!ATTLISTtitlelangCDATA#required>
]>
<bookstore>
<bookcategory="COOKING">
<title>EverydayItalian</title>
<author>GiadaDeLaurentiis</author>
<year>2005</year>
<price>30.00</price>
</book>
<bookcategory="CHILDREN">
<title>HarryPotter</title>
<author>JK.Rowling</author>
<year>2004</year>
<price>29.99</price>
</book>
<bookcategory="WEB">
<title>学习XML</title>
<author>ErikT.Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>

Schema约束 demo

booksto.xml

<?xmlversion="1.0"encoding="UTF-8"?>
<bookstorexmlns="http://www.example.org/demo"
xsi:schemaLocation="http://www.example.org/demodemo.xsd"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<bookcategory="COOKING">
<title>EverydayItalian</title>
<author>GiadaDeLaurentiis</author>
<year>2005-10-10</year>
<price>30.00</price>
</book>
<bookcategory="CHILDREN">
<title>HarryPotter</title>
<author>JK.Rowling</author>
<year>2004-10-10</year>
<price>29.99</price>
</book>
<bookcategory="WEB">
<title>学习XML</title>
<author>ErikT.Ray</author>
<year>2003-10-10</year>
<price>39.95</price>
</book>
</bookstore>

demo.xsd

<?xmlversion="1.0"encoding="UTF-8"?>
<schemaxmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/demo"
elementFormDefault="qualified">
<elementname="bookstore">
<complexType>
<sequencemaxOccurs="3"minOccurs="1">
<elementname="book">
<complexType>
<sequence>
<elementname="title">
<complexType>
<simpleContent>
<extensionbase="string">
<attributename="lang"type="string"></attribute>
</extension>
</simpleContent>
</complexType>
</element>
<elementname="author"type="string"></element>
<elementname="year"type="date"></element>
<elementname="price"type="double"></element>
</sequence>
<attributename="category"type="string"use="required"></attribute>
</complexType>
</element>
</sequence>

</complexType>
</element>
</schema>

Dom4J解析XML文件 demo

packagedemo;

importjava.util.Iterator;

importorg.dom4j.Document;
importorg.dom4j.Element;
importorg.dom4j.io.SAXReader;

publicclasstest{
	publicstaticvoidmain(String[]args)throwsException{
		//创建解析xml的对象
		SAXReaderreader=newSAXReader();
		//读取相应的xml文件
		Documentdocument=reader.read("Webroot/DTD/bookstore.xml");
		//得到相应的根元素
		Elementroot=document.getRootElement();
		for(Iteratori=root.elementIterator();i.hasNext();){
				Elementelement=(Element)i.next();
				System.out.println(element);
		}
	}

}

Dom4J 解析XML文件 demo02

packagedom4jj;

importjava.util.Iterator;
importjava.util.List;

importorg.dom4j.Attribute;
importorg.dom4j.Document;
importorg.dom4j.Element;
importorg.dom4j.io.SAXReader;

publicclasstest{
	publicstaticvoidmain(String[]args)throwsException{
		SAXReaderreader=newSAXReader();
		Documentdocument=reader.read("Webroot/schemademo/book.xml");
		Elementroot=document.getRootElement();
		
		System.out.println("-----(得到相应的根元素)-----");
		for(Iteratori=root.elementIterator();i.hasNext();){
			Elementelement=(Element)i.next();
			System.out.println(element);
		}
		
		System.out.println("----(得到相应的子元素)----");
		List<Element>els=root.elements();
		//得到第二个元素
		Elementelem=els.get(1);
		System.out.println(elem.getName());//得到相应标签的名字
		Attributeattribute=elem.attribute("category");//得到指定属性属性对象
		System.out.println(attribute.getValue());//得到相对应的属性的值
		StringattributeValue=elem.attributeValue("category");//直接得到指定属性属性值
		System.out.println(attributeValue);
		Elementelement=elem.element("title");//得到指定的标签对象
	Stringtext=element.getText();//得到相应的标签的文本值
	System.out.println(text);
	StringelementText=elem.elementText("title");
	System.out.println(elementText);
	}
}

Xpath 解析XML文件 test01

packagecn.itcast.dom4j;

importjava.util.List;

importorg.dom4j.Document;
importorg.dom4j.DocumentException;
importorg.dom4j.Element;
importorg.dom4j.io.SAXReader;

publicclassDomXpath{
publicstaticvoidmain(String[]args)throwsException{
		
	//创建解析的对象
	SAXReaderreader=newSAXReader();
//读取xml文件
	Documentdocument=reader.read("Webroot/dom4j/bookstore.xml");
	//BBB
	List<Element>elements=document.selectNodes("//year");
	for(Elementele:elements){
		
		System.out.println(ele.getText());
	}
	}
}

Xpath 解析XML文件 test02

packagecn.itcast.dom4j;

importorg.dom4j.Document;
importorg.dom4j.DocumentException;
importorg.dom4j.Element;
importorg.dom4j.Node;
importorg.dom4j.io.SAXReader;

publicclassDomXpath2{
publicstaticvoidmain(String[]args)throwsException{
	
	//获取解析xml的对象
	SAXReaderread=newSAXReader();
	//读取相应的xml文件
	Documentdocument=read.read("Webroot/dom4j/bookstore.xml");
	//查找单个标签元素//BBB[@name='bbb']
	Elementele=(Element)document.selectSingleNode("//book[@category='WEB']/price");
	System.out.println(ele.getText());
}
}

Web应用目录结构:

WEB-INF 必须有的目录,里面存放的是项目编译后的.class文件,所以不可以将.jsp文件放到该目录下

http:超文本传输协议(HTTPHyperText Transfer Protocol)

协议:规范。(我们网上传输的资源遵循http协议规范

相关文章

php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念