bookstore1.xmljava
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE bookstore [ <!ELEMENT bookstore (book+)> <!ELEMENT book (title,author,year,price)> <!ELEMENT title (#PCDATA)> <!ELEMENT author (#PCDATA)> <!ELEMENT year (#PCDATA)> <!ELEMENT price (#PCDATA)> <!ATTLIST book category CDATA #REQUIRED> <!ATTLIST title lang CDATA #REQUIRED> ]> <bookstore> <book category="COOKING"> <title>Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005</year> <price>30.00</price> </book> <book category="CHILDREN"> <title>Harry Potter</title> <author>J K. Rowling</author> <year>2004</year> <price>29.99</price> </book> <book category="WEB"> <title>學習 XML</title> <author>Erik T. Ray</author> <year>2003</year> <price>39.95</price> </book> </bookstore>
booksto.xml
dom
<?xml version="1.0" encoding="UTF-8"?> <bookstore xmlns="http://www.example.org/demo" xsi:schemaLocation="http://www.example.org/demo demo.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> <book category="COOKING"> <title>Everyday Italian</title> <author>Giada De Laurentiis</author> <year>2005-10-10</year> <price>30.00</price> </book> <book category="CHILDREN"> <title>Harry Potter</title> <author>J K. Rowling</author> <year>2004-10-10</year> <price>29.99</price> </book> <book category="WEB"> <title>學習 XML</title> <author>Erik T. Ray</author> <year>2003-10-10</year> <price>39.95</price> </book> </bookstore>
demo.xsd
jsp
<?xml version="1.0" encoding="UTF-8"?> <schema xmlns="http://www.w3.org/2001/XMLSchema" targetNamespace="http://www.example.org/demo" elementFormDefault="qualified"> <element name="bookstore"> <complexType> <sequence maxOccurs="3" minOccurs="1"> <element name="book"> <complexType> <sequence> <element name="title"> <complexType> <simpleContent> <extension base="string"> <attribute name="lang" type="string"></attribute> </extension> </simpleContent> </complexType> </element> <element name="author" type="string"></element> <element name="year" type="date"></element> <element name="price" type="double"></element> </sequence> <attribute name="category" type="string" use="required"></attribute> </complexType> </element> </sequence> </complexType> </element> </schema>
package demo; import java.util.Iterator; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.io.SAXReader; public class Test { public static void main(String[] args) throws Exception { //建立解析xml的對象 SAXReader reader = new SAXReader(); //讀取相應的xml文件 Document document = reader.read("WebRoot/DTD/bookstore.xml"); //獲得相應的根元素 Element root = document.getRootElement(); for( Iterator i = root.elementIterator(); i.hasNext();){ Element element = (Element) i.next(); System.out.println(element); } } }
package dom4jj; import java.util.Iterator; import java.util.List; import org.dom4j.Attribute; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.io.SAXReader; public class Test { public static void main(String[] args) throws Exception { SAXReader reader = new SAXReader(); Document document = reader.read("WebRoot/schemademo/book.xml"); Element root = document.getRootElement(); System.out.println("-----(獲得相應的根元素)-----"); for(Iterator i = root.elementIterator();i.hasNext();){ Element element = (Element)i.next(); System.out.println(element); } System.out.println("----(獲得相應的子元素)----"); List<Element> els = root.elements(); //獲得第二個元素 Element elem = els.get(1); System.out.println(elem.getName());//獲得相應標籤的名字 Attribute attribute = elem.attribute("category");//獲得指定屬性的屬性對象 System.out.println(attribute.getValue());//獲得相對應的屬性的值 String attributeValue = elem.attributeValue("category");//直接獲得指定屬性的屬性值 System.out.println(attributeValue); Element element = elem.element("title"); //獲得指定的標籤對象 String text = element.getText();//獲得相應的標籤的文本值 System.out.println(text); String elementText = elem.elementText("title"); System.out.println(elementText); } }
package cn.itcast.dom4j; import java.util.List; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.io.SAXReader; public class DomXpath { public static void main(String[] args) throws Exception { //建立解析的對象 SAXReader reader = new SAXReader(); //讀取xml文件 Document document = reader.read("WebRoot/dom4j/bookstore.xml"); //BBB List<Element> elements = document.selectNodes("//year"); for(Element ele:elements){ System.out.println(ele.getText()); } } }
package cn.itcast.dom4j; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.Node; import org.dom4j.io.SAXReader; public class DomXpath2 { public static void main(String[] args) throws Exception { //獲取解析xml的對象 SAXReader read = new SAXReader(); //讀取相應的xml文件 Document document = read.read("WebRoot/dom4j/bookstore.xml"); //查找單個標籤元素 //BBB[@name='bbb'] Element ele = (Element) document.selectSingleNode("//book[@category='WEB']/price"); System.out.println(ele.getText()); } }
WEB-INF 必須有的目錄,裏面存放的是項目編譯後的.class文件,因此不能夠將.jsp文件放到該目錄下學習
協議:規範。(咱們網上傳輸的資源遵循http協議規範)ui