使用Java将标签从xml文件导入到另一个

问题描述 投票:0回答:1

我完成了这部分工作,问题很简单。只是更改转帐地点。我不知道负责确定第一内容或最后内容中的运输地点的是什么问题。

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = null;
Document doc = null;
Document doc2 = null;
String a = "E:\\1.xml" ;
String  c ;
try {
    db = dbf.newDocumentBuilder();
    doc = db.parse(new File(a));
    doc2 = db.parse(new File("E:\\L (1).xml"));
    NodeList ndListFirstFile = doc.getElementsByTagName("med");
    Node nodeArea = doc.importNode(doc2.getElementsByTagName("end").item(0), true);
    NodeList nList2 = doc2.getElementsByTagName("end");
    for (int i = f; i <g; i++) {
        c = +i+"" ;
        doc2 = db.parse(new File("E:\\L ("+c+").xml"));
        for (int temp = 0; temp < nList2.getLength(); temp++) {
            nodeArea = doc.importNode(doc2.getElementsByTagName("end").item(temp), true);
             ndListFirstFile.item(0).appendChild(nodeArea);
        }  
    } 

这是通过两个文件完成的,并且效果很好,但是传输标记的位置位于内容的末尾。我想在内容的开头

<med>
I move the Tag "dat" and it is moved at the end of the Tag "med" content
<dat>We have come to the wrong place, my friend</dat></med>

<med><dat>We want to get better here</dat>
I want to move Tag dat
To be the first content from Tag med
</med>

就是这样

java xml getelementsbytagname nodelist
1个回答
1
投票

来自appendChild文档:

将节点newChild添加到该节点的子级列表的末尾。

因此它正在按预期方式添加到末尾。

要在该节点上的任何其他元素之前插入它,您可以尝试:

ndListFirstFile.item(0).insertBefore(nodeArea, ndListFirstFile.item(0).getFirstChild());
© www.soinside.com 2019 - 2024. All rights reserved.