XML一般用來存儲數據作為數據存儲的一種格式
XML還可以用於作為軟件的參數配置
這裡講下如何理解java中的相對路徑
在java中如何獲取當前的路徑,當我們知道當前的路徑了以後我們就可以使用絕對路徑了
<code>package
cn.jd.server.basic;import
java.io.IOException;import
javax.xml.parsers.ParserConfigurationException;import
javax.xml.parsers.SAXParser;import
javax.xml.parsers.SAXParserFactory;import
org.xml.sax.Attributes;import
org.xml.sax.SAXException;import
org.xml.sax.helpers.DefaultHandler;public
class
XmlTest01
{public
static
void
main
(String[] args)
throws
ParserConfigurationException, SAXException, IOException { System.out.println(); SAXParserFactory factory=SAXParserFactory.newInstance(); SAXParser parse=factory.newSAXParser(); PHandler handler=new
PHandler(); parse.parse(Thread.currentThread().getContextClassLoader() .getResourceAsStream("cn/jd/server/basic/p.xml"
),handler); } }class
PHandler
extends
DefaultHandler
{public
void
startDocument
()
throws
SAXException { System.out.println("解析文檔開始"
); }public
void
startElement
(String uri, String localName, String qName, Attributes attributes)
throws
SAXException { System.out.println(qName+"-->解析開始"
); }public
void
characters
(
char
[] ch,int
start,int
length)throws
SAXException { String contents=new
String(ch,start,length).trim();if
(contents.length()>0
) { System.out.println("內容為:"
+contents); }else
{ System.out.println("空內容"
); } }public
void
endElement
(String uri, String localName, String qName)
throws
SAXException { System.out.println(qName+"-->解析結束"
); }public
void
endDocument
()
throws
SAXException { System.out.println("解析文檔結束"
); } } /<code>
<code><
persons
><
person
><
name
>至尊寶name
><
age
>9000age
>person
><
person
><
name
>白晶晶name
><
age
>7000age
>person
>persons
>/<code>
關鍵字: 解析 SAXException public