Java 操作xml有那种能像操作数据的sql语句那样操作的么?有的话,请详细解释下,或者留个链接,多谢..

Java操作xml有那种能像操作数据的sql语句那样操作的么?有的话,请详细解释下,或者留个链接,多谢..... Java 操作xml有那种能像操作数据的sql语句那样操作的么?有的话,请详细解释下,或者留个链接,多谢.. 展开
 我来答
百度网友6fe74e2
2013-03-25 · TA获得超过6593个赞
知道大有可为答主
回答量:1973
采纳率:100%
帮助的人:263万
展开全部
可以用DOM(文件对象模型)来生成或者解析xml文件。
给你个代码例子,看明白了就差不多了:
import java.io.FileInputStream; import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
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;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
/** ** DOM生成与解析XML文档 */
public class JavaTestXMLFile{
private Document document;
private String fileName;
public void init()
{
try
{
DocumentBuilderFactory factory = DocumentBuilderFactory .newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
this.document = builder.newDocument();
}
catch (ParserConfigurationException e)
{
System.out.println(e.getMessage());
}
}
public void createXml(String fileName)
{
Element root = this.document.createElement("employees");
this.document.appendChild(root);
Element employee = this.document.createElement("employee");
Element name = this.document.createElement("name");
name.appendChild(this.document.createTextNode("employee1"));
employee.appendChild(name);
Element sex = this.document.createElement("sex");
sex.appendChild(this.document.createTextNode("m"));
employee.appendChild(sex);
Element age = this.document.createElement("age");
age.appendChild(this.document.createTextNode("25"));
employee.appendChild(age); root.appendChild(employee);
TransformerFactory tf = TransformerFactory.newInstance();
try
{
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);
System.out.println("生成XML文件成功!");
}
catch (TransformerConfigurationException e)
{
System.out.println(e.getMessage());
}
catch (IllegalArgumentException e)
{
System.out.println(e.getMessage());
}
catch (FileNotFoundException e)
{
System.out.println(e.getMessage());
}
catch (TransformerException e)
{
System.out.println(e.getMessage());
}
}
public void parserXml(String fileName)
{
try
{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(fileName);
NodeList employees = document.getChildNodes();
for (int i = 0; i < employees.getLength(); i++)
{
Node employee = employees.item(i);
NodeList employeeInfo = employee.getChildNodes();
for (int j = 0; j < employeeInfo.getLength(); j++)
{
Node node = employeeInfo.item(j);
NodeList employeeMeta = node.getChildNodes();
for (int k = 0; k < employeeMeta.getLength(); k++)
{
System.out.println(employeeMeta.item(k).getNodeName() + ":" + employeeMeta.item(k).getTextContent());
}
}
}
System.out.println("解析完毕");
}
catch (FileNotFoundException e)
{
System.out.println(e.getMessage());
}
catch (ParserConfigurationException e)
{
System.out.println(e.getMessage());
}
catch (SAXException e)
{
System.out.println(e.getMessage());
}
catch (IOException e)
{
System.out.println(e.getMessage());
}
}

public static void main(String[] args) {
JavaTestXMLFile myJavaTestXMLFile=new JavaTestXMLFile();
myJavaTestXMLFile.init();
myJavaTestXMLFile.createXml("DomCreateXml.xml");
myJavaTestXMLFile.parserXml("DomCreateXml.xml");
}

}
EvilProxy
2013-03-26 · TA获得超过294个赞
知道小有建树答主
回答量:280
采纳率:100%
帮助的人:263万
展开全部
dom4j

可以使用xPath来获取节点,比如 /root/factory/woker
用法很简单。
可参考

http://www.ibm.com/developerworks/cn/xml/x-dom4j.html
http://www.blogjava.net/i369/articles/154264.html
http://sourceforge.net/projects/dom4j/
来自:求助得到的回答
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式