I have list of tournaments in my a.xml:
我的a.xml中有锦标赛列表:
<tournaments>
<tournament>
<name>a</name>
</tournament>
<tournament>
<name>b</name>
</tournament>
<tournament>
<name>c</name>
</tournament>
</tournaments>
ad then I have one tournament in b.xml
广告然后我在b.xml中有一个锦标赛
<tournament>
<name>d</name>
</tournament>
How I can apend document b.xml to a.xml into as another tournament ?
我如何将b.xml的文档作为另一个锦标赛添加到a.xml中?
so this is what I want:
所以这就是我想要的:
<tournaments>
<tournament>
<name>a</name>
</tournament>
<tournament>
<name>b</name>
</tournament>
<tournament>
<name>c</name>
</tournament>
<tournament>
<name>d</name>
</tournament>
</tournaments>
2 个解决方案
#1
6
- Get Node to add from first Document;
- Adopt Node (see Document.adopt(Node)) from first Document to the second Document;
- Appent adopted Node as a child to second Document structure (see Node.appendChild(Node).
从第一个文档中添加节点;
从第一个Document到第二个Document采用Node(参见Document.adopt(Node));
将所采用的节点作为子节点添加到第二个Document结构中(请参阅Node.appendChild(Node)。
Update. Code:
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));
Result:
<?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>
#2
0
Will this work for you?
这对你有用吗?
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;
}
}
You can ammend the getXXXX() functons suite your own code
你可以修改getXXXX()functons套件你自己的代码
#1
6
- Get Node to add from first Document;
- Adopt Node (see Document.adopt(Node)) from first Document to the second Document;
- Appent adopted Node as a child to second Document structure (see Node.appendChild(Node).
从第一个文档中添加节点;
从第一个Document到第二个Document采用Node(参见Document.adopt(Node));
将所采用的节点作为子节点添加到第二个Document结构中(请参阅Node.appendChild(Node)。
Update. Code:
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));
Result:
<?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>
#2
0
Will this work for you?
这对你有用吗?
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;
}
}
You can ammend the getXXXX() functons suite your own code
你可以修改getXXXX()functons套件你自己的代码