大橙子网站建设,新征程启航

为企业提供网站建设、域名注册、服务器等服务

java代码写xml文件,xml文件 java

JAVA如何写XML文件?

import java.io.*;\x0d\x0a \x0d\x0aimport org.dom4j.*;\x0d\x0a import org.dom4j.io.OutputFormat;\x0d\x0a import org.dom4j.io.XMLWriter;\x0d\x0a \x0d\x0apublic class DOM4JTest {\x0d\x0a public static void main(String[] args) {\x0d\x0a Document doc = DocumentHelper.createDocument();\x0d\x0a doc.addProcessingInstruction("xml-stylesheet", "type='text/xsl href='students.xsl'");\x0d\x0a Element root = doc.addElement("students");\x0d\x0a \x0d\x0a Element eltStu1 = root.addElement("student").addAttribute("sn", "01");\x0d\x0a Element eltName1 = eltStu1.addElement("name");\x0d\x0a Element eltAge1 = eltStu1.addElement("age");\x0d\x0a eltName1.setText("张三");\x0d\x0a eltAge1.setText("20");\x0d\x0a \x0d\x0a Element eltStu2 = root.addElement("student").addAttribute("sn", "02");\x0d\x0a Element eltName2 = eltStu2.addElement("name");\x0d\x0a Element eltAge2 = eltStu2.addElement("age");\x0d\x0a eltName2.setText("李四");\x0d\x0a eltAge2.setText("18");\x0d\x0a \x0d\x0a try {\x0d\x0a OutputFormat format = new OutputFormat("\x0d\x0a ", true);\x0d\x0a format.setEncoding("gb2312");\x0d\x0a // 可以把System.out改为你要的流。\x0d\x0a XMLWriter xmlWriter = new XMLWriter(new PrintWriter(System.out), format);\x0d\x0a xmlWriter.write(doc);\x0d\x0a xmlWriter.close();\x0d\x0a } catch (IOException e) {\x0d\x0a e.printStackTrace();\x0d\x0a }\x0d\x0a }\x0d\x0a }

成都创新互联公司网络公司拥有10余年的成都网站开发建设经验,1000多家客户的共同信赖。提供网站制作、网站建设、网站开发、网站定制、卖友情链接、建网站、网站搭建、成都响应式网站建设、网页设计师打造企业风格,提供周到的售前咨询和贴心的售后服务

java怎样读写和修改XML文件??

xml:

?xml version="1.0" encoding="UTF-8"?

users

Messages

sendNamesendUsers/sendName

receiveNamesnake/receiveName

date2007-12-04 12:20:00/date

status0/status

messagethis is Content/message

/Messages

/users

java:

package com.lianxi.DAO;

import java.io.File;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.transform.Transformer;

import javax.xml.transform.TransformerFactory;

import javax.xml.transform.dom.DOMSource;

import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;

import org.w3c.dom.Node;

import org.w3c.dom.NodeList;

public class UpdateXml {

public static boolean doc2XmlFile(Document document,String filename)

{

boolean flag = true;

try

{

/** 将document中的内容写入文件中 */

TransformerFactory tFactory = TransformerFactory.newInstance();

Transformer transformer = tFactory.newTransformer();

/** 编码 */

//transformer.setOutputProperty(OutputKeys.ENCODING, "GB2312");

DOMSource source = new DOMSource(document);

StreamResult result = new StreamResult(new File(filename));

transformer.transform(source, result);

}catch(Exception ex)

{

flag = false;

ex.printStackTrace();

}

return flag;

}

public static Document load(String filename)

{

Document document = null;

try

{

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

DocumentBuilder builder=factory.newDocumentBuilder();

document=builder.parse(new File(filename));

document.normalize();

}

catch (Exception ex){

ex.printStackTrace();

}

return document;

}

/**

* 演示修改文件的具体某个节点的值

*/

public static void xmlUpdateDemo()

{

Document document = load("c://Message.xml");

Node root=document.getDocumentElement();

/** 如果root有子元素 */

if(root.hasChildNodes())

{

/** ftpnodes */

NodeList ftpnodes = root.getChildNodes();

/** 循环取得ftp所有节点 */

for (int i=0;iftpnodes.getLength();i++)

{

NodeList ftplist = ftpnodes.item(i).getChildNodes();

for (int k=0;kftplist.getLength();k++)

{

Node subnode = ftplist.item(k);

/** 删除ftp-chn节点 */

// if (subnode.getNodeType()==Node.ELEMENT_NODEsubnode.getNodeName()=="ftp-chn")

// {

// ftpnodes.item(i).removeChild(subnode);

// }

/** 修改ftp-host的值为 192.168.0.1 */

if (subnode.getNodeType()==Node.ELEMENT_NODEsubnode.getNodeName()=="status")

{

subnode.getFirstChild().setNodeValue("1");

}

}

}

}

doc2XmlFile(document,"c://Message.xml");

}

public static void main(String args[])throws Exception

{

UpdateXml.xmlUpdateDemo();

}

}

如何用java代码创建xml文件

用java自带的就可以,有问题可以问我

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

//构造

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如何写?

这个是要求命令行程序吧

首先是选择一个xml库,然后是实现基本的集合查询,最后是实现输入输出

Java编写xml文件

老弟啊 你这些代码太多啊 我给你指出个方向吧,你不就是想作出来个树状菜单吗?树状菜单的生成有很多方法,这里我给你介绍4个,第一用dhtmlxtree,具体怎么弄官方给出了例子,第二dtree,这个是由3部分组成,一个是JS文件,一个是CSS文件,一个是你页面调用DAO层方法显示页面的文件,第三用acegi,这个是跟STRUTS相关的组件,第四就是直接通过JAVA out.println()出来,这里你让谁谁都不会给你完整的写出来,因为代码量大,所以我只能给你指明下方向

如何用java生成一个xml文件

一个XML文档,可以先构造一个DOM,然后将DOM转化为xml序列,输出或者生成文件。package test;

import java.io.ByteArrayOutputStream;

import java.io.File;

import java.io.FileOutputStream;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.transform.Transformer;

import javax.xml.transform.TransformerFactory;

import javax.xml.transform.dom.DOMSource;

import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;

import org.w3c.dom.Element;

public class Test {

public static void generate(){

try {

DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

DocumentBuilder builder = factory.newDocumentBuilder();

Document document = builder.newDocument();

document.setXmlVersion("1.0");

document.setXmlStandalone(true);

Element root = document.createElement_x("MobileNet"); //创建根节点

document.appendChild(root); //将根节点添加到Document对象中

Element pageElement = document.createElement_x("page"); //设置第一个page元素到

pageElement.setAttribute("name", "list.jsp"); //设置page节点的name属性

Element methodElement = document.createElement_x("method"); //设置method节点

methodElement.setTextContent("get"); //给method设置值

pageElement.appendChild(methodElement); //添加method节点到page节点内

Element displayElement = document.createElement_x("display"); //设置method节点

displayElement.setTextContent("list撒旦发放"); //给display设置值

pageElement.appendChild(displayElement); //添加display节点到page节点内

Element request_paramElement = document.createElement_x("request_param");

request_paramElement.setTextContent("request_param1|request_param2");

pageElement.appendChild(request_paramElement);

root.appendChild(pageElement);

pageElement = document.createElement_x("page"); //设置第二个page元素到

pageElement.setAttribute("name", "content.jsp"); //设置page节点的name属性

methodElement = document.createElement_x("method");

methodElement.setTextContent("post");

pageElement.appendChild(methodElement);

displayElement = document.createElement_x("display");

displayElement.setTextContent("content");

pageElement.appendChild(displayElement);

Element url_titleElement = document.createElement_x("url_title"); //设置url_title节点

url_titleElement.setTextContent("title,publisher,published_calendar"); //给url_title设置值

pageElement.appendChild(url_titleElement); //添加url_title节点到page节点内

root.appendChild(pageElement); //将page段加人根节点内

TransformerFactory transFactory = TransformerFactory.newInstance(); //开始把Document映射到文件

Transformer transFormer = transFactory.newTransformer();

DOMSource domSource = new DOMSource(document); //设置输出结果

File file = new File("MobileNetRule.xml"); //生成xml文件

if (!file.exists()) {

file.createNewFile();

}

FileOutputStream out = new FileOutputStream(file); //文件输出流

StreamResult xmlResult = new StreamResult(out); //设置输入源

transFormer.transform(domSource, xmlResult); //输出xml文件

System.out.println(file.getAbsolutePath()); //测试文件输出的路径

TransformerFactory tf = TransformerFactory.newInstance();

Transformer t = tf.newTransformer();

t.setOutputProperty("{/encoding/}","GB2312/");

ByteArrayOutputStream boc = new ByteArrayOutputStream();

t.transform(new DOMSource(document), new StreamResult(boc));

String xmlstring = boc.toString();

System.out.println(xmlstring);

} catch (Exception e) {

e.printStackTrace();

}

}

public static void main(String[] args){

Test.generate();

}

}


本文名称:java代码写xml文件,xml文件 java
文章网址:http://dzwzjz.com/article/dscgded.html
在线咨询
服务热线
服务热线:028-86922220
TOP