使用Java创建复杂的XMl文件

时间:2014-03-20 13:06:49

标签: java xml

我需要使用java创建一个XML文件,我得到了我喜欢的文件但是我在每个结束标记之前都缺少关系标记,我怎么能得到它 预期文件:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<FlyBoy>
    <learJet>CL-215</learJet>
    <rank>2</rank>
    <FlyBoy>
        <viper>Mark II</viper>
        <rank>1</rank>
        <FlyBoy>
            <viper>Mark II4455</viper>
            <rank>2</rank>
            <FlyBoy>
                <viper>Mark II56666</viper>
                <rank>3</rank>
                <relation name="Date" table="Sam"/>
            </FlyBoy>
            <relation name="Date" table="Mark"/>
        </FlyBoy>
        <relation name="Date" table="sechma"/>
    </FlyBoy>
    <relation name="Date" table="John"/>
</FlyBoy>

我得到的输出:

<FlyBoy><learJet>CL-215</learJet><rank>2</rank><FlyBoy><viper>Mark II</viper><rank>1</rank><FlyBoy><viper>Mark II4455</viper><rank>2</rank><FlyBoy><viper>Mark II56666</viper><rank>3</rank></FlyBoy></FlyBoy></FlyBoy></FlyBoy>

代码:

    import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;
import org.w3c.dom.Element;

public class XmlGenerator {



    /**
     * Render flyboy
     * 
     */
    private Element renderFlyBoy(Element parent, String viper, String rank) {

        Element flyBoyEl = document.createElement("FlyBoy");
        parent.appendChild(flyBoyEl);

        Element viperEl = document.createElement("viper");
        viperEl.setTextContent(viper);
        flyBoyEl.appendChild(viperEl);

        Element rankEl = document.createElement("rank");
        rankEl.setTextContent(rank);
        flyBoyEl.appendChild(rankEl);

        return flyBoyEl;

    }

    // Test
    public static void main(String[] args) {

        try {
                Document document = null;
                Element root = null;  

                DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
                document = documentBuilderFactory.newDocumentBuilder().newDocument();

                 root = document.createElement("FlyBoy");
                document.appendChild(root);

                Element learJet = document.createElement("learJet");
                learJet.setTextContent("CL-215");
                root.appendChild(learJet);

                 Element rank = document.createElement("rank");
                 rank.setTextContent("2");
                 root.appendChild(rank);

                Element flyBoy1 = renderFlyBoy(root, "Mark II", "1");
                Element flyBoy2 = renderFlyBoy(flyBoy1, "Mark II4455", "2");
                Element flyBoy3 = renderFlyBoy(flyBoy2, "Mark II56666", "3");

                Element relation_schema= doc.createElement("relation");
                 flyBoy1.appendChild(relation_schema);

                 Attr join2 = doc.createAttribute("name");
                 join2.setValue("Date");
                 relation_schema.setAttributeNode(join2);

                  Attr type = doc.createAttribute("table");
                  type.setValue("xxxxxx");
                  relation_schema.setAttributeNode(type); 


                DOMSource domSource = new DOMSource(document);
                Transformer transformer = TransformerFactory.newInstance().newTransformer();
                transformer.setOutputProperty(OutputKeys.INDENT, "yes");
                transformer.setOutputProperty(OutputKeys.METHOD, "xml");
                StreamResult result = new StreamResult(new File("my.xml"));
                transformer.transform(domSource, result);

        } catch (ParserConfigurationException | TransformerFactoryConfigurationError | TransformerException e) {
            e.printStackTrace();
        }
            System.out.println("done...");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}

1 个答案:

答案 0 :(得分:0)

renderFlyBoy方法中,您需要以与创建其他方式相同的方式创建relation。然后将在FlyBoy

中添加关系
 Element rankEl = document.createElement("relation");
 // code to add attributes
 flyBoyEl.appendChild(rankEl);