2011-11-01 84 views
4

我在比赛A.XML名单:如何从XML文档节点追加对现有的XML文档

<tournaments> 

    <tournament> 
     <name>a</name> 
    </tournament> 

    <tournament> 
     <name>b</name> 
    </tournament> 

    <tournament> 
     <name>c</name> 
    </tournament> 

</tournaments> 

广告,然后我有一个B.XML比赛

<tournament> 
    <name>d</name> 
</tournament> 

如何我可以将文档b.xml添加到a.xml中作为另一个比赛?

所以这就是我想要的:

<tournaments> 

    <tournament> 
     <name>a</name> 
    </tournament> 

    <tournament> 
     <name>b</name> 
    </tournament> 

    <tournament> 
     <name>c</name> 
    </tournament> 

    <tournament> 
     <name>d</name> 
    </tournament> 

</tournaments> 
+0

您正在使用哪种xml解析器? – janoliver

+0

dom它设置为标记 – hudi

回答

6
  1. 获得Node从第一Document增加;
  2. 采用Node(见Document.adopt(Node))从第一个Document到第二个Document;
  3. Appent采用Node作为一个孩子第二Document结构(见Node.appendChild(Node)

更新 代码:。

DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); 
DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder(); 

Document tournament = builder.parse(new File("b.xml")); 
Document tournaments = builder.parse(new File("a.xml")); 

Node tournamentElement = tournament.getFirstChild(); 
Node ndetournament = tournaments.getDocumentElement(); 
Node firstDocImportedNode = tournaments.adoptNode(tournamentElement); 
ndetournament.appendChild(firstDocImportedNode); 

TransformerFactory transformerFactory = TransformerFactory.newInstance(); 
Transformer transformer = transformerFactory.newTransformer(); 
transformer.setOutputProperty(OutputKeys.INDENT, "yes"); 
transformer.transform(new DOMSource(tournaments), new StreamResult(System.out)); 

结果:

<?xml version="1.0" encoding="UTF-8" standalone="no"?> 
<tournaments> 
    <tournament> 
     <name>a</name> 
    </tournament> 

    <tournament> 
     <name>b</name> 
    </tournament> 

    <tournament> 
     <name>c</name> 
    </tournament> 
<tournament> 
    <name>d</name> 
</tournament> 
</tournaments> 
+0

我尝试这个,但没有成功:Document tournament = builder.parse(new File(“b.xml”)); Document tournaments = builder.parse(new File(“a.xml”)); Element tournamentElement =(Element)tournament.getFirstChild(); (元素)tournaments.getElementsByTagName(“turnaje”)。item(0);元素ndetournament =(元素) 节点firstDocImportedNode = tournaments.adoptNode(tournamentElement); ndetournament.appendChild(firstDocImportedNode); – hudi

+0

你会得到什么例外? – svaor

+0

好的这个工作。我没有写更改xml文件现在使用transformerFactory现在它是好的 – hudi

0
请问

这对你的工作?

import java.io.StringBufferInputStream; 

import javax.xml.parsers.DocumentBuilder; 
import javax.xml.parsers.DocumentBuilderFactory; 

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

public class Tournament { 

    private static final String tournamentData = 
    " <tournaments>" + 
    " <tournament>" + 
    "  <name>a</name>" + 
    " </tournament>" + 
    " <tournament>" + 
    "  <name>b</name>" + 
    " </tournament>" + 
    " <tournament>" + 
    "  <name>c</name>" + 
    " </tournament>" + 
    "</tournaments>"; 


    private static final String tournamentB = 
    " <tournament>" + 
    "  <name>d</name>" + 
    " </tournament>"; 

    private static DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); 

    public static void main(String[] args) { 
    try { 
     Document currentTournaments = getCurrentTournaments(); 
     Element tournament = getNewTournament(); 
     Element ndetournament = (Element) currentTournaments.getElementsByTagName("tournaments").item(0); 
     Node firstDocImportedNode = currentTournaments.importNode(tournament, true); 
     ndetournament.appendChild(firstDocImportedNode); 
    } catch (Exception e) { 
     e.printStackTrace(); 
    } 
    } 

    private static Document getCurrentTournaments() throws Exception{ 
    DocumentBuilder builder = dbFactory.newDocumentBuilder(); 
    Document docTournament = builder.parse(new StringBufferInputStream(tournamentData)); 
    return docTournament; 
    } 

    private static Element getNewTournament() throws Exception{ 
    DocumentBuilder builder = dbFactory.newDocumentBuilder(); 
    Document newTournament = builder.parse(new StringBufferInputStream(tournamentData)); 
    Element tournament = newTournament.getDocumentElement(); 
    return tournament; 
    } 
} 

您可以修改getXXXX()函数套件您自己的代码

+0

nope我尝试这个,但没有成功:文档锦标赛= builder.parse(新文件(“b.xml”)); Document tournaments = builder.parse(new File(“a.xml”)); Element tournamentElement =(Element)tournament.getFirstChild(); (元素)tournaments.getElementsByTagName(“turnaje”)。item(0);元素ndetournament =(元素) 节点firstDocImportedNode = tournaments.importNode(tournamentElement,true); ndetournament.appendChild(firstDocImportedNode); – hudi

+0

“b.xml”在哪里我已经使用新文件(“c:\\ a.xml”)重新测试,它似乎工作。 – GrahamA