在java中读xml
在java中写xml
import
javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Element;
public class ReadXML_Can ... {
public static void main(String[] args) throws Exception ...{
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = documentBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse("candidate.xml");
NodeList nList = doc.getElementsByTagName("person");
for(int i =0 ;i<nList.getLength();i++)...{
Element el = (Element)nList.item(i);
String personid = el.getAttribute("personId");
String name = el.getElementsByTagName("name").item(0).getFirstChild().getNodeValue();
String address = el.getElementsByTagName("address").item(0).getFirstChild().getNodeValue();
String tel = el.getElementsByTagName("tel").item(0).getFirstChild().getNodeValue();
String fax = el.getElementsByTagName("fax").item(0).getFirstChild().getNodeValue();
String email = el.getElementsByTagName("email").item(0).getFirstChild().getNodeValue();
System.out.println("--- personid ="+personid);
System.out.println("--- name ="+name);
System.out.println("--- address ="+address);
System.out.println("--- tel ="+tel);
System.out.println("--- fax ="+fax);
System.out.println("--- email ="+email +" ");
}
}
}
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Element;
public class ReadXML_Can ... {
public static void main(String[] args) throws Exception ...{
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = documentBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse("candidate.xml");
NodeList nList = doc.getElementsByTagName("person");
for(int i =0 ;i<nList.getLength();i++)...{
Element el = (Element)nList.item(i);
String personid = el.getAttribute("personId");
String name = el.getElementsByTagName("name").item(0).getFirstChild().getNodeValue();
String address = el.getElementsByTagName("address").item(0).getFirstChild().getNodeValue();
String tel = el.getElementsByTagName("tel").item(0).getFirstChild().getNodeValue();
String fax = el.getElementsByTagName("fax").item(0).getFirstChild().getNodeValue();
String email = el.getElementsByTagName("email").item(0).getFirstChild().getNodeValue();
System.out.println("--- personid ="+personid);
System.out.println("--- name ="+name);
System.out.println("--- address ="+address);
System.out.println("--- tel ="+tel);
System.out.println("--- fax ="+fax);
System.out.println("--- email ="+email +" ");
}
}
}
import
javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import java.sql. * ;
import org.w3c.dom.Element;
import org.apache.xml.serialize.XMLSerializer;
import org.apache.xml.serialize.OutputFormat;
import java.io.FileOutputStream;
import java.io.OutputStream;
public class WriterXML_User ... ... {
public static void main(String[] args) ......{
Connection conn =null;
Statement stmt = null;
ResultSet rs = null;
Element person;
Element username;
Element pwd;
Element email;
Element homepage;
Element regtime;
Element money;
DocumentBuilderFactory documentbuilderfactory = DocumentBuilderFactory.newInstance();
try......{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection("jdbc:odbc:xml");
stmt = conn.createStatement();
rs = stmt.executeQuery("select * from userinfo");
DocumentBuilder docBuilder = documentbuilderfactory.newDocumentBuilder();
Document doc = docBuilder.newDocument();
Element rootElement = doc.createElement("personinfos");
while(rs.next())......{
person = doc.createElement("person");
person.setAttribute("personid",rs.getString("id"));
rootElement.appendChild(person);
username = doc.createElement("username");
username.appendChild(doc.createTextNode(rs.getString("username")));
person.appendChild(username);
pwd = doc.createElement("password");
pwd.appendChild(doc.createTextNode(rs.getString("password")));
person.appendChild(pwd);
email = doc.createElement("email");
email.appendChild(doc.createTextNode(rs.getString("email")));
person.appendChild(email);
homepage = doc.createElement("homepage");
homepage.appendChild(doc.createTextNode(rs.getString("homepage")));
person.appendChild(homepage);
regtime = doc.createElement("regtime");
regtime.appendChild(doc.createTextNode(rs.getString("regtime")));
person.appendChild(regtime);
money = doc.createElement("money");
money.appendChild(doc.createTextNode(rs.getString("money")));
person.appendChild(money);
}
doc.appendChild(rootElement);
//格式化XML 文档
OutputFormat outformat = new OutputFormat(doc);
//通过输出流创建XML
OutputStream os = new FileOutputStream("personinfo.xml");
// 通过XMLSerializer 生成XML 文档
XMLSerializer xmlserilizer = new XMLSerializer(os,outformat);
// 对文件进行写操作.
xmlserilizer.serialize(doc);
}catch(Exception ex)......{
ex.printStackTrace();
}
}
}
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import java.sql. * ;
import org.w3c.dom.Element;
import org.apache.xml.serialize.XMLSerializer;
import org.apache.xml.serialize.OutputFormat;
import java.io.FileOutputStream;
import java.io.OutputStream;
public class WriterXML_User ... ... {
public static void main(String[] args) ......{
Connection conn =null;
Statement stmt = null;
ResultSet rs = null;
Element person;
Element username;
Element pwd;
Element email;
Element homepage;
Element regtime;
Element money;
DocumentBuilderFactory documentbuilderfactory = DocumentBuilderFactory.newInstance();
try......{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn = DriverManager.getConnection("jdbc:odbc:xml");
stmt = conn.createStatement();
rs = stmt.executeQuery("select * from userinfo");
DocumentBuilder docBuilder = documentbuilderfactory.newDocumentBuilder();
Document doc = docBuilder.newDocument();
Element rootElement = doc.createElement("personinfos");
while(rs.next())......{
person = doc.createElement("person");
person.setAttribute("personid",rs.getString("id"));
rootElement.appendChild(person);
username = doc.createElement("username");
username.appendChild(doc.createTextNode(rs.getString("username")));
person.appendChild(username);
pwd = doc.createElement("password");
pwd.appendChild(doc.createTextNode(rs.getString("password")));
person.appendChild(pwd);
email = doc.createElement("email");
email.appendChild(doc.createTextNode(rs.getString("email")));
person.appendChild(email);
homepage = doc.createElement("homepage");
homepage.appendChild(doc.createTextNode(rs.getString("homepage")));
person.appendChild(homepage);
regtime = doc.createElement("regtime");
regtime.appendChild(doc.createTextNode(rs.getString("regtime")));
person.appendChild(regtime);
money = doc.createElement("money");
money.appendChild(doc.createTextNode(rs.getString("money")));
person.appendChild(money);
}
doc.appendChild(rootElement);
//格式化XML 文档
OutputFormat outformat = new OutputFormat(doc);
//通过输出流创建XML
OutputStream os = new FileOutputStream("personinfo.xml");
// 通过XMLSerializer 生成XML 文档
XMLSerializer xmlserilizer = new XMLSerializer(os,outformat);
// 对文件进行写操作.
xmlserilizer.serialize(doc);
}catch(Exception ex)......{
ex.printStackTrace();
}
}
}