将JTree转换为XML [英] Convert JTree to XML

查看:138
本文介绍了将JTree转换为XML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看过很多关于如何将XML读取到JTree中的文章,但是关于如何从JTree创建XML的文章很少.有人可以通过简单的方法帮助我吗?我看过一个看起来像这样的例子:

I've seen many many articles on how to read XML into a JTree but few on how to create the XML from the JTree. Can anyone help me with a simple approach for this? I've seen an example that looked like:

 XMLEncoder e = new XMLEncoder(
                new BufferedOutputStream(new FileOutputStream(f.toString())));
        e.writeObject(o);
        e.close();

..但是我无法使它正常工作;它返回一个XML文件,但它并不十分正确,如下所示:

.. but I can't get this to work; it returns an XML file but its not quite right, looking like this:

<java version="1.6.0_17" class="java.beans.XMLDecoder"> 
 <object class="javax.swing.JTree"> 
  <object class="javax.swing.tree.DefaultTreeModel"> 
   <object class="javax.swing.tree.DefaultMutableTreeNode"> 
    <void property="userObject"> 

..等,但其中没有我的数据.

.. etc, but with none of my data in there.

(PS:请保持温柔,我对Java非常陌生!)

(PS: Please be gentle, I'm very new to java!)

推荐答案

XMLEncoder是用于将bean编码为文本的通用实用程序.我认为这不适合您的情况.

The XMLEncoder is a generic utility for encoding beans as text. I don't think it is suitable in your case.

假设我很了解您的需求,我编写了一段代码即可完成这项工作.您只需要将树模型作为参数传递给toXml方法.请注意,这只是草稿;您可能希望以不同的方式处理异常,并以不同的方式管理转换参数.更重要的是,您可以操纵递归createTree方法,以更改每个树节点创建的XML节点的结构.

I wrote a piece of code that does the job, assuming that I understand well your needs. You only have to pass the tree model as a parameter to the toXml method. Note that this is just a draft; You will probably want to handle exceptions differently, and manage your transformation parameters differently. More important, you can manipulate the recursive createTree method in order to change the structure of the XML node created per tree node.

public static String toXml(TreeModel model) throws ParserConfigurationException, TransformerException {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    DOMImplementation impl = builder.getDOMImplementation();

    // Build an XML document from the tree model
    Document doc = impl.createDocument(null,null,null);
    Element root = createTree(doc, model, model.getRoot());
    doc.appendChild(root);

    // Transform the document into a string
    DOMSource domSource = new DOMSource(doc);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.METHOD, "xml");
    transformer.setOutputProperty(OutputKeys.ENCODING,"UTF-8");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    StringWriter sw = new StringWriter();
    StreamResult sr = new StreamResult(sw);
    transformer.transform(domSource, sr);
    return sw.toString();
}

private static Element createTree(Document doc, TreeModel model, Object node) {
    Element el = doc.createElement(node.toString());
    for(int i=0;i<model.getChildCount(node);i++){
        Object child = model.getChild(node, i);
        el.appendChild(createTree(doc,model,child));
    }
    return el;
}

这篇关于将JTree转换为XML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆