java 写 xml

  • Z3_799491
    了解作者
  • 1KB
    文件大小
  • rar
    文件格式
  • 0
    收藏次数
  • VIP专享
    资源类型
  • 0
    下载次数
  • 2022-04-12 04:45
    上传日期
java 写xml文件 WriteXmlTemp myxml1 = new WriteXmlTemp("testTemp1.xml"); for(int i=0;i<1000;i++){ myxml1.toWriteTemp(String.valueOf(i), "abc.pcm"); myxml1.toSave(); } System.out.println(System.currentTimeMillis()); System.out.println("Your writing is successful."); ……
WriteXmlTemp.rar
  • WriteXmlTemp.java
    3.1KB
内容介绍
package bus.com.data; import org.w3c.dom.*; import javax.xml.parsers.*; import javax.xml.transform.*; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import java.io.*; public class WriteXmlTemp { private Document document; private String filename; Element root; public WriteXmlTemp(String name) throws ParserConfigurationException { filename = name; DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); document = builder.newDocument(); root = document.createElement("AudioFileName"); document.appendChild(root); } 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(); } } /* 将ChooseAudioJFrame下的语音文件保存到 tempXML 中; * */ public void toWriteTemp(String ID, String FileName) { Element note=document.createElement("note"); root.appendChild(note); Element id = document.createElement("ID"); id.appendChild(document.createTextNode(ID)); note.appendChild(id); Element fname = document.createElement("FileName"); fname.appendChild(document.createTextNode(FileName)); note.appendChild(fname); } public static void main(String args[]) { try { WriteXmlTemp myxml1 = new WriteXmlTemp("f:\\testTemp.xml"); for(int i=0;i<1000;i++){ myxml1.toWriteTemp(String.valueOf(i), "abc.pcm"); myxml1.toSave(); } System.out.println(System.currentTimeMillis()); System.out.println("Your writing is successful."); } catch (ParserConfigurationException exp) { exp.printStackTrace(); System.out.print("Your writing is failed."); } try { WriteXmlTemp myxml2 = new WriteXmlTemp("f:\\testTemp2.xml"); for(int i=0;i<1000;i++){ myxml2.toWriteTemp(String.valueOf(i), "abc.pcm"); } myxml2.toSave(); System.out.println(System.currentTimeMillis()); System.out.println("Your writing is successful."); } catch (ParserConfigurationException exp) { exp.printStackTrace(); System.out.print("Your writing is failed."); } } }
评论
    相关推荐