XML结构创建问题

时间:2009-12-26 23:49:28

标签: java xml

我的数据格式如下(HashMap列表)

{TeamName=India, Name=Sachin, Score=170}
{TeamName=India, Name=Sehwag, Score=120}
{TeamName=Sri-Lanka, Name=Sangakara, Score=20}
{TeamName=Sri-Lanka, Name=Murali, Score=20}
{TeamName=Sri-Lanka, Name=Jayasurya, Score=70}

以下是所需的输出

<node id="1" label="India" >
        <node id="1.1" label="Sachin" Score="170" />
        <node id="1.2" label="Sehwag" Score="120" />
</node>
<node id="2" label="Sri-Lanka">
      <node id="2.1" label="Sangakara" Score="20" />
      <node id="2.2" label="Murali" Score="20" />
      <node id="2.3" label="Jayasurya" Score="70" />
</node>
<node id="3" label="World-XI">
      <node id="2.2" label="Murali" Score="20" />
      <node id="1.1" label="Sachin" Score="170" />
</node>

现在,我已经能够生成上述结构,但是有问题,“node id = 3”中的id需要重复,而不是重新创建为3.1 / 3.2。

另一个是,它不一定是节点3是最后一个,我只能迭代它,但可能有许多其他节点。

以下是上面的一段代码,有什么建议吗?

Map hm = new HashMap();
            Element em = null;
            try {
                int serverId = 0;
                int clientId = 0;

                DocumentBuilderFactory documentBuilderFactory = 
                DocumentBuilderFactory.newInstance();
                DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
                Document document = documentBuilder.newDocument();
                Element rootElement = document.createElement("CricketDetails");
                document.appendChild(rootElement);

                for (int i=0; i < l.size(); i++) {
                    hm = (HashMap) l.get(i);
                    sortListIP.add(hm.get("TeamName"));     
                }

                Collections.sort(sortListIP);

                HashSet h = new HashSet(sortListIP);
                sortListIP.clear();
                sortListIP.addAll(h);

                for (int i=0; i < sortListIP.size(); i++) {
                    ++serverId;
                    clientId = 0;
                    em = document.createElement("node");
                    em.setAttribute("id", ""+serverId);
                    em.setAttribute("TeamName", ""+sortListIP.get(i));
                    for (int j=0; j < l.size();j++) {
                        hm = (HashMap) l.get(j);

                        if (sortListIP.get(i).equals(hm.get("TeamName"))) {
                            Element em_child = document.createElement("node");
                            ++clientId;
                            em_child.setAttribute("id", serverId+"."+clientId);
                            em_child.setAttribute("label", (String) hm.get("Name"));
                            em.appendChild(em_child);
                            rootElement.appendChild(em);
                        }
                    }

                }
                TransformerFactory transformerFactory = TransformerFactory.newInstance();
                Transformer transformer = transformerFactory.newTransformer();
                DOMSource source = new DOMSource(document);
                StreamResult result =  new StreamResult("cricketDetails.xml");
                transformer.transform(source, result);

P.S。定义的格式可能不正确,但只需要这样的预期输出!

1 个答案:

答案 0 :(得分:1)

如果要修改使用代码生成的节点的属性id,请相应地修改包含em_child.setAttribute("id", serverId+"."+clientId);的行。父节点id的生成方式类似。

我不确定您的xml是否只是不完整,但在XML中您需要拥有根或文档实体。

  

“[XML]文档以”root“开头   或文件实体。“ - http://www.w3.org/TR/REC-xml/#sec-documents