public class Xml{
private Element e;
Node[] tagNode;
Node[] proNode;
private String[] tagNameList;
private String[] proNameList;
//set xml's object from xml's name
public void xmlObj(String name) {
try{
DocumentBuilderFactory dof=DocumentBuilderFactory.newInstance();
DocumentBuilder db=dof.newDocumentBuilder();
Document document = db.parse(new File(name));
e = document.getDocumentElement();
}
catch(Exception e){
System.out.println("file's Exception");
}
}
//get xml's object
public Element getElement(){
System.out.println(e);
return e;
}
/**
//set tag's name by tagNode
public void setTag(String eleName){
Element el = getElement();
int length = el.getElementsByTagName(eleName).getLength();
tagNode = new Node[length];
int loop;
for(loop = 0;loop < length;loop++){
tagNode[loop] = el.getElementsByTagName(eleName).item(loop);
System.out.println(tagNode[loop]);
}
}
//get tag's name from tagNode
public Node[] getTag(){
return tagNode;
}
**/
//set tag's name by tagNameList
public void setTagName(String name) {
try{
//this.name = name;
//xml.xmlObj("Tag.xml");
int length = e.getElementsByTagName("tag").getLength();
tagNameList = new String[length];
for(int loop = 0;loop < length;loop++){
tagNameList[loop] = e.getElementsByTagName("tag").item(loop).getAttributes().getNamedItem(name).getNodeValue();
System.out.println(tagNameList[loop]);
}
//for(int step = 0;step<nameList.length;step++)
//System.out.println(nameList[step]);
}
catch(Exception e){
System.out.println(e);
}
}
//get tag's name from tagNameList
public String[] getTagName(){
// System.out.println(nameList[0]);
return tagNameList;
// Error generated by the parser
System.out.println("\n** Parsing error"
+ ", line " + spe.getLineNumber()
+ ", uri " + spe.getSystemId());
System.out.println(" " + spe.getMessage() );
// Use the contained exception, if any
Exception x = spe;
if (spe.getException() != null)
x = spe.getException();
x.printStackTrace();
}
catch (SAXException sxe) {
// Error generated during parsing
Exception x = sxe;
if (sxe.getException() != null)
x = sxe.getException();
x.printStackTrace();
}
catch (ParserConfigurationException pce) {
// Parser with specified options can't be built
pce.printStackTrace();
}
catch (IOException ioe) {
// I/O error
ioe.printStackTrace();
}
}
//get property's name from tag's name
public String getPro(){
// System.out.println(proListName[1]);
// return proListName;
return "hehehehhehehe";
}
public static void main(String args[]) throws Exception{
Xml xml = new Xml();
xml.xmlObj("g:/hyper editor/Tag.xml");
//xml.getElement();
xml.setTagName("name");
xml.getTagName();
//xml.setPro("tag","name");
xml.setPro("Table");
}
下面这是个写XML的,不过是基于GUI的,道理是一样嘛!
===============================================
要导入
import javax.xml.parsers.*;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
才行
private void saveData(){
String DES ;
try{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.newDocument();
// Create DOM Tree
Element root = document.createElement("AimsLicense");
document.appendChild(root);
Element Company = document.createElement("Company");
Element ComInfo = document.createElement("ComInfo");
Element UseInfo = document.createElement("UseInfo");
Element OthInfo = document.createElement("OthInfo");
root.appendChild(Company);
Element NameLong = document.createElement("NameLong");
Element NameShort = document.createElement("NameShort");
Element Location = document.createElement("Location");
Element Address = document.createElement("Address");
Element TermDate = document.createElement("TermDate");
Element Contact = document.createElement("Contact");
Element ContactMail = document.createElement("ContactMail");
Element IssueDate = document.createElement("IssueDate");
Element UserNum = document.createElement("UserNum");
Element PassDue = document.createElement("PassDue");
Element LicEnc = document.createElement("LicEnc");
Element SoftwareName = document.createElement("SoftwareName");
Element ComponentName = document.createElement("ComponentName");
Element SoftekMail = document.createElement("SoftekMail");
DES = LicenseAdmin.getInstance().LicenseEnc(txtLong.getText()+txtTerm.getText()+txtUser.getText()+txtPass.getText()+txtSoftek.getText()+txtSoft.getText()+txtCompo.getText());