标签:
一、 怎么样去写一个xml
<!ELEMENT BookStore (book+)>
<!ELEMENT book (name,author,price)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT price (#PCDATA)>
<!ATTLIST book bookname CDATA #REQUIRED>
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE BookStore [
<!ELEMENT BookStore (book+)>
<!ELEMENT book (name,author,price)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT author (#PCDATA)>
<!ELEMENT price (#PCDATA)>
<!ATTLIST book bookname CDATA #REQUIRED>
]>
<BookStore>
<book bookname="JavaEE">
<name><![CDATA[java入门到精通]]></name>
<author>张三</author>
<price>998</price>
</book>
<book bookname="android">
<name><![CDATA[ANDROID入门到精通]]></name>
<author>张三</author>
<price>998</price>
</book>
</BookStore>
<!DOCTYPE BookStore SYSTEM "BOOK.dtd">
<?xml version="1.0" encoding="UTF-8"?>
<schema xmlns="http://www.w3.org/2001/XMLSchema"
targetNamespace="http://www.example.org/demo1"
xmlns:tns="http://www.example.org/demo1"
elementFormDefault="qualified">
<element name="BookStore">
<complexType><!-- 复杂的内容如果有子标签就需要写这个 -->
<sequence maxOccurs="1"><!-- 顺序标签 minOccurs:代表最少出现 maxOccurs最大出现次数 参数可以是unbounded代表无限次 -->
<element name="book">
<complexType>
<sequence>
<element name="name" type="string" ></element>
<element name="author" type="string" ></element>
<element name="price" type="string" ></element>
</sequence>
<attribute name="bookname" type="string" use="optional"></attribute>
</complexType>
</element>
</sequence>
</complexType>
</element>
</schema>
<BookStore
xmlns="http://www.example.org/demo1"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.example.org/demo1 demo1.xsd"
>
// (1)创建解析器
SAXReader sr=new SAXReader();
// (2)加载需要解析文件
String path = Demo4j.class.getClassLoader().getResource("MyXml.xml").getPath();
File f=new File(path);
Document dou= sr.read(f);
// (3)获取根标签元素
Element root=dou.getRootElement();
// (4)根据需求去解析想要的数据
List <Element> list=root.elements();
for(Element e:list)
{
System.out.println(e.elementText("name"));
}
import java.util.ArrayList;
import java.util.List;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserFactory;
public class Pull
{
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception
{
// TODO Auto-generated method stub
List<Book> li = null;
Book b = null;
// (1) 创建解析器工厂
XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
// (2) 使用工厂去制造一个解析器
XmlPullParser xp = factory.newPullParser();
// (3) 将需要解析的文档放到解析器中
xp.setInput(Pull.class.getClassLoader()
.getResourceAsStream("MyXml.xml"), "UTF-8");
// (4) 进行解析
// 需求:将文档中的数据保存到一个集合
int type = 0;
while ((type = xp.getEventType()) != XmlPullParser.END_DOCUMENT)
{
String name = xp.getName();
System.out.println(name);
switch (type)
{
case XmlPullParser.START_TAG:
if ("BookStore".equals(name))
{
li = new ArrayList<Book>();
}
else if ("book".equals(name))
{
b = new Book();
b.setBookname(xp.getAttributeValue(0));
}
else if ("name".equals(name))
{
b.setName(xp.nextText());
}
else if ("author".equals(name))
{
b.setAuthor(xp.nextText());
}
else if ("price".equals(name))
{
b.setPrice(Integer.parseInt(xp.nextText()));
}
break;
case XmlPullParser.END_TAG:
if ("book".equals(name))
{
li.add(b);
}
break;
}
xp.next();
}
for (Book book : li)
{
System.out.println(book);
}
}
}
标签:
原文地址:http://www.cnblogs.com/didixyy/p/47e9e2003f9f13595e66830da3841c91.html