2011-11-01 115 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