再csdn上搜搜,很多!http://expert.csdn.net/Expert/topic/955/955989.xml?temp=.3712427
http://expert.csdn.net/Expert/topic/1066/1066616.xml?temp=.143078
http://expert.csdn.net/Expert/topic/823/823676.xml?temp=.5197412
http://expert.csdn.net/Expert/topic/955/955989.xml?temp=.3712427
http://expert.csdn.net/Expert/topic/1066/1066616.xml?temp=.143078
http://expert.csdn.net/Expert/topic/823/823676.xml?temp=.5197412
http://expert.csdn.net/Expert/topic/955/955989.xml?temp=.3712427
import javax.xml.parsers.*;
import org.w3c.dom.*;DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Logger.global.info("Load settings from " + config_fullpath);
Document doc = db.parse(config_fullpath);
//get DB connection string
{
NodeList nl = doc.getElementsByTagName("connectionstring");
Node conn_node = nl.item(0);
DBConnString = conn_node.getFirstChild().getNodeValue(); }
//get port number
{
NodeList nl = doc.getElementsByTagName("portnumber");
Node conn_node = nl.item(0);
port = Integer.parseInt(conn_node.getFirstChild().getNodeValue());
}<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<connectionstring>jdbc:microsoft:sqlserver://192.168.0.2:1433;user=sa;password=sa;DatabaseName=test</connectionstring>
<portnumber>1000</portnumber>
</configuration>
XML入门
http://www-900.ibm.com/developerWorks/cn/cnedu.nsf/xml-onlinecourse-bytitle/098067B621EEBFBFC8256C80002A2DF6?OpenDocument
理解DOM
http://www-900.ibm.com/developerWorks/cn/cnedu.nsf/xml-onlinecourse-bytitle/386674F65A47844C48256BD10023D453?OpenDocument
import java.beans.*;public class XMLTest {
public void xmlEncode() throws Exception
{
MyInfo3 my = new MyInfo3();
my.setMIAge(25);
my.setMStrMyName("google");
my.setMStrMyAddr("china");
my.setEducation("好的了"); XMLEncoder encoder = new XMLEncoder(
new BufferedOutputStream(new FileOutputStream("myinfo.xml")));
encoder.writeObject(my);
encoder.close();
System.out.println(my);
} public void xmlDecode() throws Exception
{
java.beans.XMLDecoder decoder = new java.beans.XMLDecoder(
new BufferedInputStream(new FileInputStream("myinfo.xml")));
MyInfo3 my = (MyInfo3)decoder.readObject();
decoder.close();
System.out.println(my);
System.out.println(my.getMStrMyName());
System.out.println(my.getMStrMyAddr());
}
public static void main (String args[]) throws Exception {
XMLTest st = new XMLTest();
st.xmlEncode();
st.xmlDecode();
}
}
{
private int mIAge;
private String mStrMyName;
private String mStrMyAddr;
private String education;
private String sex;
public MyInfo3()
{
} public void setMIAge(int mIAge)
{
this.mIAge = mIAge;
}
public int getMIAge()
{
return mIAge;
}
public void setMStrMyName(String mStrMyName)
{
this.mStrMyName = mStrMyName;
}
public String getMStrMyName()
{
return mStrMyName;
}
public void setMStrMyAddr(String mStrMyAddr)
{
this.mStrMyAddr = mStrMyAddr;
}
public String getMStrMyAddr()
{
return mStrMyAddr;
}
public void setEducation(String education)
{
this.education = education;
}
public String getEducation()
{
return education;
}
public void setSex(String sex)
{
this.sex = sex;
}
public String getSex()
{
return sex;
}}
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.xml.sax.*;public class DomXmlParse {
String str="";
Document doc = null;
public void parseXML() throws ParserConfigurationException,IOException,SAXException{
//getXMLString(); InputStream inputStream = new ByteArrayInputStream(str.getBytes());
InputSource inputSource = new InputSource(inputStream);
DocumentBuilderFactory docBuilderFac = DocumentBuilderFactory.newInstance();
docBuilderFac.setValidating(true);
System.out.println(docBuilderFac.isValidating());
DocumentBuilder docBuilder = docBuilderFac.newDocumentBuilder();
doc = docBuilder.parse(inputSource);
} private String getXMLString(){
try {
InputStream inputstream = new FileInputStream("E:\\pizza.xml"); int c = 0;
while ((c = inputstream.read()) != -1) {
str = str + (char)c; }
}
catch (Exception ex) {
System.out.println(ex.getMessage());
}
return str;
}
public void getNodeValue() {
NodeList nodelist = doc.getElementsByTagName("pizza");
for (int i = 0; i < nodelist.getLength(); i++) {
System.out.println(nodelist.item(i).getNodeName());
NodeList nodelist2 = nodelist.item(i).getChildNodes();
for (int j = 0; j < nodelist2.getLength(); j++) {
if(nodelist2.item(j).getNodeType() == Node.ELEMENT_NODE) {
System.out.print(" " + nodelist2.item(j).getNodeName() + ": ");
if(nodelist2.item(j).getChildNodes() != null)
System.out.println(nodelist2.item(j).getChildNodes().item(0).getNodeValue());
}
}
} }
public static void main(String[] args) throws Exception {
DomXmlParse parser = new DomXmlParse();
parser.parseXML();
parser.getNodeValue();
}
}附xml文件内容:<?xml version="1.0"?>
<!DOCTYPE pizzamenu [
<!ELEMENT pizzamenu (pizza+)>
<!ELEMENT pizza (topping, price, size)>
<!ELEMENT topping (#PCDATA)>
<!ELEMENT price (#PCDATA)>
<!ELEMENT size (#PCDATA)>
<!ATTLIST topping
extracheese (yes | no) "no"
>
]>
<pizzamenu>
<pizza>
<topping extracheese="yes">Pepperoni</topping>
<price> 12.99 </price>
<size> large </size>
</pizza>
<pizza>
<topping extracheese="yes">Cheese</topping>
<price> 9.99 </price>
<size> nomal </size>
</pizza>
<!--hello-->
<pizza>
<topping extracheese="no">Sausage</topping>
<price> 11.99 </price>
<size> small </size>
</pizza>
<pizza>
<topping extracheese="no">Mushroom</topping>
<price> 14.99 </price>
<size> small </size>
</pizza>
<pizza>
<topping extracheese="yes">Onion</topping>
<price> 8.99 </price>
<size> big </size>
</pizza>
</pizzamenu>