新聞中心
java中根據(jù)xml模板創(chuàng)建xml
/**
安義ssl適用于網(wǎng)站、小程序/APP、API接口等需要進行數(shù)據(jù)傳輸應(yīng)用場景,ssl證書未來市場廣闊!成為創(chuàng)新互聯(lián)的ssl證書銷售渠道,可以享受市場價格4-6折優(yōu)惠!如果有意向歡迎電話聯(lián)系或者加微信:13518219792(備注:SSL證書合作)期待與您的合作!
* 復(fù)制單個文件
* @param oldPath String 原文件路徑 如:c:/fqf.txt
* @param newPath String 復(fù)制后路徑 如:f:/fqf.txt
* @return boolean
*/
public void copyFile(String oldPath, String newPath) {
try {
int bytesum = 0;
int byteread = 0;
File oldfile = new File(oldPath);
if (oldfile.exists()) { //文件存在時
InputStream inStream = new FileInputStream(oldPath); //讀入原文件
FileOutputStream fs = new FileOutputStream(newPath);
byte[] buffer = new byte[1444];
int length;
while ( (byteread = inStream.read(buffer)) != -1) {
bytesum += byteread; //字節(jié)數(shù) 文件大小
System.out.println(bytesum);
fs.write(buffer, 0, byteread);
}
inStream.close();
}
}
catch (Exception e) {
System.out.println("復(fù)制單個文件操作出錯");
e.printStackTrace();
}
}
xml怎么寫java代碼
xml中是不能寫java代碼的。
jsp中之所以能夠?qū)慾ava代碼,是因為jsp在返回給客戶端的時候,會經(jīng)由jsp解析器進行解析,執(zhí)行,最終返回給客戶端,而xml文件是不會出現(xiàn)這種操作的。
如果想使用代碼片段的話,建議你使用模版來進行處理。
關(guān)于模版的技術(shù),現(xiàn)在比較有名的就是velocity開源項目,你可以上網(wǎng)查詢一些,如果有興趣可以私下交流,這兒說不清楚
如何用java代碼創(chuàng)建xml文件
用java自帶的就可以,有問題可以問我
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
//構(gòu)造
public XMLUtil(String name) throws ParserConfigurationException {
filename = name;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
builder = factory.newDocumentBuilder();
document = builder.newDocument();
}
/**
* 保存到文件
*/
public void toSave() {
try {
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
DOMSource source = new DOMSource(document);
transformer.setOutputProperty(OutputKeys.ENCODING, "GB2312");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
PrintWriter pw = new PrintWriter(new FileOutputStream(filename));
StreamResult result = new StreamResult(pw);
transformer.transform(source, result);
} catch (TransformerException mye) {
mye.printStackTrace();
} catch (IOException exp) {
exp.printStackTrace();
}
}
JAVA 生成xml格式,具體格式如下,請問JAVA方法怎么寫
import java.io.File;import java.io.FileNotFoundException;import java.io.FileOutputStream;import java.io.IOException;import org.w3c.dom.*;import org.xml.sax.SAXException;import javax.xml.parsers.*;import javax.xml.transform.*;import javax.xml.transform.dom.DOMSource;import javax.xml.transform.stream.*;import javax.xml.xpath.*;public class Test { public static void main(String[] args) { DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance(); Element theBook=null, theElem=null, root=null; try { factory.setIgnoringElementContentWhitespace(true); DocumentBuilder db=factory.newDocumentBuilder(); Document xmldoc=db.parse(new File("Test1.xml")); root=xmldoc.getDocumentElement(); theBook=(Element) selectSingleNode("/books/book[name='哈里波特']", root); System.out.println("--- 查詢找《哈里波特》 ----"); Element nameNode=(Element)theBook.getElementsByTagName("price").item(0); String name=nameNode.getFirstChild().getNodeValue(); System.out.println(name); output(theBook); System.out.println("=============selectSingleNode(books/book[name='哈里波特'], root)=================="); //--- 新建一本書開始 ---- theBook=xmldoc.createElement("book"); theElem=xmldoc.createElement("name"); theElem.setTextContent("新書"); theBook.appendChild(theElem); theElem=xmldoc.createElement("price"); theElem.setTextContent("20"); theBook.appendChild(theElem); theElem=xmldoc.createElement("memo"); theElem.setTextContent("新書的更好看。"); theBook.appendChild(theElem); root.appendChild(theBook); System.out.println("--- 新建一本書開始 ----"); output(xmldoc); System.out.println("=============================="); //--- 新建一本書完成 ---- //--- 下面對《哈里波特》做一些修改。 ---- //--- 查詢找《哈里波特》---- //--- 此時修改這本書的價格 ----- theBook.getElementsByTagName("price").item(0).setTextContent("15");//getElementsByTagName返回的是NodeList,所以要跟上item(0)。另外,getElementsByTagName("price")相當(dāng)于xpath的".//price"。 System.out.println("--- 此時修改這本書的價格 ----"); output(theBook); //--- 另外還想加一個屬性id,值為B01 ---- theBook.setAttribute("id", "B01"); System.out.println("--- 另外還想加一個屬性id,值為B01 ----"); output(theBook); //--- 對《哈里波特》修改完成。 ---- //--- 要用id屬性刪除《三國演義》這本書 ---- theBook=(Element) selectSingleNode("/books/book[@id='B02']", root); System.out.println("--- 要用id屬性刪除《三國演義》這本書 ----"); output(theBook); theBook.getParentNode().removeChild(theBook); System.out.println("--- 刪除后的XML ----"); output(xmldoc); //--- 再將所有價格低于10的書刪除 ---- NodeList someBooks=selectNodes("/books/book[price10]", root); System.out.println("--- 再將所有價格低于10的書刪除 ---"); System.out.println("--- 符合條件的書有 "+someBooks.getLength()+"本。 ---"); for(int i=0;isomeBooks.getLength();i++) { someBooks.item(i).getParentNode().removeChild(someBooks.item(i)); } output(xmldoc); saveXml("Test1_Edited.xml", xmldoc); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (SAXException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } public static void output(Node node) {//將node的XML字符串輸出到控制臺 TransformerFactory transFactory=TransformerFactory.newInstance(); try { Transformer transformer = transFactory.newTransformer(); transformer.setOutputProperty("encoding", "gb2312"); transformer.setOutputProperty("indent", "yes"); DOMSource source=new DOMSource(); source.setNode(node); StreamResult result=new StreamResult(); result.setOutputStream(System.out); transformer.transform(source, result); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } } public static Node selectSingleNode(String express, Object source) {//查找節(jié)點,并返回第一個符合條件節(jié)點 Node result=null; XPathFactory xpathFactory=XPathFactory.newInstance(); XPath xpath=xpathFactory.newXPath(); try { result=(Node) xpath.evaluate(express, source, XPathConstants.NODE); } catch (XPathExpressionException e) { e.printStackTrace(); } return result; } public static NodeList selectNodes(String express, Object source) {//查找節(jié)點,返回符合條件的節(jié)點集。 NodeList result=null; XPathFactory xpathFactory=XPathFactory.newInstance(); XPath xpath=xpathFactory.newXPath(); try { result=(NodeList) xpath.evaluate(express, source, XPathConstants.NODESET); } catch (XPathExpressionException e) { e.printStackTrace(); } return result; } public static void saveXml(String fileName, Document doc) {//將Document輸出到文件 TransformerFactory transFactory=TransformerFactory.newInstance(); try { Transformer transformer = transFactory.newTransformer(); transformer.setOutputProperty("indent", "yes"); DOMSource source=new DOMSource(); source.setNode(doc); StreamResult result=new StreamResult(); result.setOutputStream(new FileOutputStream(fileName)); transformer.transform(source, result); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } catch (FileNotFoundException e) { e.printStackTrace(); } }} XML:?xml version="1.0" encoding="GBK"?booksbookname哈里波特/nameprice10/pricememo這是一本很好看的書。/memo/bookbook id="B02"name三國演義/nameprice10/pricememo四大名著之一。/memo/bookbook id="B03"name水滸/nameprice6/pricememo四大名著之一。/memo/bookbook id="B04"name紅樓/nameprice5/pricememo四大名著之一。/memo/book/books
新聞標(biāo)題:java代碼xml模板 java根據(jù)模板生成xml文件
文章來源:http://www.ef60e0e.cn/article/dochgpi.html