java如何写xml文件_java – 如何读写xml文件?

这是一个快速DOM示例,演示如何使用其dtd读取和写入简单的xml文件:

User

Author

Admin

和dtd:

先导入这些:

import javax.xml.parsers.*;

import javax.xml.transform.*;

import javax.xml.transform.dom.*;

import javax.xml.transform.stream.*;

import org.xml.sax.*;

import org.w3c.dom.*;

以下是您需要的一些变量:

private String role1 = null;

private String role2 = null;

private String role3 = null;

private String role4 = null;

private ArrayList rolev;

这是一个reader(String xml是你的xml文件的名称):

public boolean readXML(String xml) {

rolev = new ArrayList();

Document dom;

// Make an instance of the DocumentBuilderFactory

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

try {

// use the factory to take an instance of the document builder

DocumentBuilder db = dbf.newDocumentBuilder();

// parse using the builder to get the DOM mapping of the

// XML file

dom = db.parse(xml);

Element doc = dom.getDocumentElement();

role1 = getTextValue(role1, doc, "role1");

if (role1 != null) {

if (!role1.isEmpty())

rolev.add(role1);

}

role2 = getTextValue(role2, doc, "role2");

if (role2 != null) {

if (!role2.isEmpty())

rolev.add(role2);

}

role3 = getTextValue(role3, doc, "role3");

if (role3 != null) {

if (!role3.isEmpty())

rolev.add(role3);

}

role4 = getTextValue(role4, doc, "role4");

if ( role4 != null) {

if (!role4.isEmpty())

rolev.add(role4);

}

return true;

} catch (ParserConfigurationException pce) {

System.out.println(pce.getMessage());

} catch (SAXException se) {

System.out.println(se.getMessage());

} catch (IOException ioe) {

System.err.println(ioe.getMessage());

}

return false;

}

这是一位作家:

public void saveToXML(String xml) {

Document dom;

Element e = null;

// instance of a DocumentBuilderFactory

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

try {

// use factory to get an instance of document builder

DocumentBuilder db = dbf.newDocumentBuilder();

// create instance of DOM

dom = db.newDocument();

// create the root element

Element rootEle = dom.createElement("roles");

// create data elements and place them under root

e = dom.createElement("role1");

e.appendChild(dom.createTextNode(role1));

rootEle.appendChild(e);

e = dom.createElement("role2");

e.appendChild(dom.createTextNode(role2));

rootEle.appendChild(e);

e = dom.createElement("role3");

e.appendChild(dom.createTextNode(role3));

rootEle.appendChild(e);

e = dom.createElement("role4");

e.appendChild(dom.createTextNode(role4));

rootEle.appendChild(e);

dom.appendChild(rootEle);

try {

Transformer tr = TransformerFactory.newInstance().newTransformer();

tr.setOutputProperty(OutputKeys.INDENT, "yes");

tr.setOutputProperty(OutputKeys.METHOD, "xml");

tr.setOutputProperty(OutputKeys.ENCODING, "UTF-8");

tr.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, "roles.dtd");

tr.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

// send DOM to file

tr.transform(new DOMSource(dom),

new StreamResult(new FileOutputStream(xml)));

} catch (TransformerException te) {

System.out.println(te.getMessage());

} catch (IOException ioe) {

System.out.println(ioe.getMessage());

}

} catch (ParserConfigurationException pce) {

System.out.println("UsersXML: Error trying to instantiate DocumentBuilder " + pce);

}

}

getTextValue在这里:

private String getTextValue(String def, Element doc, String tag) {

String value = def;

NodeList nl;

nl = doc.getElementsByTagName(tag);

if (nl.getLength() > 0 && nl.item(0).hasChildNodes()) {

value = nl.item(0).getFirstChild().getNodeValue();

}

return value;

}

添加一些访问器和变换器,你就完成了!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值