在Java中将JTree转换为XML

时间:2016-01-11 10:48:56

标签: xml attributes jtree

我是Java的新手,遇到了问题。我有一个我想阅读的JTree。我使用下面的代码,它适用于Elements。但是我的属性和值的顺序错误,每个属性名和值都显示两次:<Product Software="Software" dfg="dfg"></Product>。它应该如下所示:<Product Software="dfg"></Product>

我该如何解决?

感谢您的帮助!

我的代码:

public class JTreeToXML {

static Document doc;

public static void writeFile(String file, TreeModel model)
        throws TransformerException, ParserConfigurationException, SAXException, IOException {

    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    DOMImplementation impl = builder.getDOMImplementation();

    doc = impl.createDocument(null, null, null);
    System.out.println("--------------------------");
    Element root = createTree(doc, model, model.getRoot(), 0);
    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-16");
    transformer.setOutputProperty(OutputKeys.INDENT, "no");
    transformer.setOutputProperty(OutputKeys.CDATA_SECTION_ELEMENTS, "expanded names");
    StreamResult sr = new StreamResult(new File(file + ".xml"));
    System.out.println(file + ".xml");
    transformer.transform(domSource, sr);

}

public static String encodeToUnicodeStandard(String pData) {

    StringBuffer encodedData = new StringBuffer();
    StringBuffer sBuff = new StringBuffer(pData);

    for (int i = 0; i < sBuff.length(); i++) {

        char ch = sBuff.charAt(i);
        int chVal = (int) ch;

        // if this is a special character (ie. > 8 bits), encode it
        if (chVal > Byte.MAX_VALUE) {

            encodedData.append("&#x").append(Integer.toHexString(chVal)).append(";");

        } else {

            encodedData.append(ch);

        }
    }

    return encodedData.toString();

}

private static Element createTree(Document doc, TreeModel model, Object node, int z) {

    Element el = doc.createElement(node.toString());

    for (int i = 0; i < model.getChildCount(node); i++) {

        Object child = model.getChild(node, i);
        // TRY kann eigentlich entfernt werden

        if (child.toString().length() != 0) {

                // Wenn kein leaf
                if (model.getChildCount(child) != 0) {

                    el.appendChild(createTree(doc, model, child, z + 1));

                } else {
                    //int a = 4; 
                    //a= a+i;
                    // Wenn ein leaf
                    if(i < model.getChildCount(node)) {
                    //el.setAttribute(child.toString(), model.getChild(node, a).toString());
                    //el.setAttribute(child.toString(), model.getChild(node, i).toString());
                    //System.out.println(child.toString() + "=" + model.getChild(node, i).toString());
                        //el.setAttribute(model.getChild(node, i).toString(), child.toString());                    
                        el.setAttribute(child.toString(), child.toString());

                    }

                }

                System.out.println(z + " + " + child.toString());

            }
        }

    return el;

}
}

0 个答案:

没有答案