哪位兄台给一个用java来创建xml文件、更新xml节点的例子?
呵呵。分数提高了哈。希望帮帮忙啊yhuaqiang@163.com。谢谢哦,如果用到了XPath,更好奥...
呵呵。分数提高了哈。希望帮帮忙啊
yhuaqiang@163.com。谢谢哦,如果用到了XPath,更好奥 展开
yhuaqiang@163.com。谢谢哦,如果用到了XPath,更好奥 展开
展开全部
java创建文件不都是一样吗?
至于更新xml文件,看下这个··
copy别人的
xml:
<?xml version="1.0" encoding="UTF-8"?>
<users>
<Messages>
<sendName>sendUsers</sendName>
<receiveName>snake</receiveName>
<date>2007-12-04 12:20:00</date>
<status>0</status>
<message>this 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;i<ftpnodes.getLength();i++)
{
NodeList ftplist = ftpnodes.item(i).getChildNodes();
for (int k=0;k<ftplist.getLength();k++)
{
Node subnode = ftplist.item(k);
/** 删除ftp-chn节点 */
// if (subnode.getNodeType()==Node.ELEMENT_NODE&&subnode.getNodeName()=="ftp-chn")
// {
// ftpnodes.item(i).removeChild(subnode);
// }
/** 修改ftp-host的值为 192.168.0.1 */
if (subnode.getNodeType()==Node.ELEMENT_NODE&&subnode.getNodeName()=="status")
{
subnode.getFirstChild().setNodeValue("1");
}
}
}
}
doc2XmlFile(document,"c://Message.xml");
}
public static void main(String args[])throws Exception
{
UpdateXml.xmlUpdateDemo();
}
}
至于更新xml文件,看下这个··
copy别人的
xml:
<?xml version="1.0" encoding="UTF-8"?>
<users>
<Messages>
<sendName>sendUsers</sendName>
<receiveName>snake</receiveName>
<date>2007-12-04 12:20:00</date>
<status>0</status>
<message>this 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;i<ftpnodes.getLength();i++)
{
NodeList ftplist = ftpnodes.item(i).getChildNodes();
for (int k=0;k<ftplist.getLength();k++)
{
Node subnode = ftplist.item(k);
/** 删除ftp-chn节点 */
// if (subnode.getNodeType()==Node.ELEMENT_NODE&&subnode.getNodeName()=="ftp-chn")
// {
// ftpnodes.item(i).removeChild(subnode);
// }
/** 修改ftp-host的值为 192.168.0.1 */
if (subnode.getNodeType()==Node.ELEMENT_NODE&&subnode.getNodeName()=="status")
{
subnode.getFirstChild().setNodeValue("1");
}
}
}
}
doc2XmlFile(document,"c://Message.xml");
}
public static void main(String args[])throws Exception
{
UpdateXml.xmlUpdateDemo();
}
}
参考资料: http://zhidao.baidu.com/question/46959510.html?si=1
展开全部
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
我有 你邮箱说下 我发给你
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询