public static void main(String args[]){ byte[] aa = "aa".getBytes(); System.out.println(aa); Document document = new Document(); Element root = new Element("Message"); root.setAttribute("vendor","wiscom"); Element event = new Element("Event"); event.setAttribute("target" , "uid"); event.setAttribute("evenTime" , "" + System.currentTimeMillis()); event.setAttribute("eventType" , "cc"); Element attributeValuePairElem = new Element("AttributeValuePair"); Element attributeElem = new Element("Attribute"); attributeElem.setAttribute("name" , "email" ); Element valueElem = new Element("Value"); valueElem.setText("[email protected]"); attributeElem.addContent(valueElem); attributeValuePairElem.addContent(attributeElem); event.addContent(attributeValuePairElem); root.addContent(event); document.setRootElement(root);
Test test = new Test(); test.doCreateXMLFile(document , "D:\\aa.xml"); } }
import org.w3c.dom.Element;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;public ParseConfig(LinkedList input , LinkedList output) {
//所要解析的xml文件
fileURI = ( (String)new File(System.getProperty("user.dir")).getPath()) +
"\\System.xml";
//////////////////System.out.println(fileURI);
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(fileURI);
Element elmt = doc.getDocumentElement();
NodeList nl = elmt.getElementsByTagName("DataSource");//DataSource是xml文件中的一个标签 for (int i = 0; i < nl.getLength(); i++) {
Element elmtCurrent = (Element) nl.item(i);
NodeList nlCurrent = elmtCurrent.getElementsByTagName("DS");
if (nlCurrent.item(0).getFirstChild().getNodeValue().equals(ds)) {
DS = nlCurrent.item(0).getFirstChild().getNodeValue();
//////////////////System.out.println(" DS is : " + DS); nlCurrent = elmtCurrent.getElementsByTagName("URL");
URL = nlCurrent.item(0).getFirstChild().getNodeValue();
//////////////////System.out.println(" URL is : " + URL); nlCurrent = elmtCurrent.getElementsByTagName("Driver");
Driver = nlCurrent.item(0).getFirstChild().getNodeValue();
//////////////////System.out.println(" Driver is : " + Driver); nlCurrent = elmtCurrent.getElementsByTagName("IP");
IP = nlCurrent.item(0).getFirstChild().getNodeValue();
//////////////////System.out.println(" IP is : " + IP); nlCurrent = elmtCurrent.getElementsByTagName("UseID");
UserID = nlCurrent.item(0).getFirstChild().getNodeValue();
//////////////////System.out.println(" UseID is : " + UserID); nlCurrent = elmtCurrent.getElementsByTagName("UsePassword");
UserPassword = nlCurrent.item(0).getFirstChild().getNodeValue();
//////////////////System.out.println(" UsePassword is : " + UserPassword);
} else
continue;
}
} catch (IOException ie) {
ie.printStackTrace();
} catch (SAXException se) {
se.printStackTrace();
} catch (ParserConfigurationException pce) {
pce.printStackTrace();
}
createcon();
process(input , output);
release();
} public void createcon(){
try {
Class.forName(Driver);
} catch (ClassNotFoundException cnfe) {
cnfe.printStackTrace();
} try {
con = DriverManager.getConnection(URL, UserID, UserPassword);
} catch (SQLException sqle) {
sqle.printStackTrace();
}
}
偶也是菜鸟
望高手指点
jdom 1.1public class Test {
/**
* 生成需要的XML文件
*
*/
private boolean doCreateXMLFile(Document doc , String filePath) {
try{
XMLOutputter xmlout= new XMLOutputter("",true,"gb2312"); //08/19 EDIT
FileOutputStream output = new FileOutputStream(filePath);
xmlout.output(doc,output);
xmlout.clone();
xmlout = null;
output.close();
output = null;
}catch(Exception e){
e.printStackTrace();
return false;
}
return true;
}
public static void main(String args[]){
byte[] aa = "aa".getBytes();
System.out.println(aa);
Document document = new Document();
Element root = new Element("Message");
root.setAttribute("vendor","wiscom");
Element event = new Element("Event");
event.setAttribute("target" , "uid");
event.setAttribute("evenTime" , "" + System.currentTimeMillis());
event.setAttribute("eventType" , "cc");
Element attributeValuePairElem = new Element("AttributeValuePair");
Element attributeElem = new Element("Attribute");
attributeElem.setAttribute("name" , "email" );
Element valueElem = new Element("Value");
valueElem.setText("[email protected]");
attributeElem.addContent(valueElem);
attributeValuePairElem.addContent(attributeElem);
event.addContent(attributeValuePairElem); root.addContent(event);
document.setRootElement(root);
Test test = new Test();
test.doCreateXMLFile(document , "D:\\aa.xml");
}
}
import org.jdom.Element;
import org.jdom.output.XMLOutputter;
Document doc = null;
SAXBuilder sb = new SAXBuilder();
doc = sb.build(xml文件);