java - 使用 JAVA 将标签从 xml 文件导入到另一个文件

标签 java xml getelementsbytagname nodelist

我用这件作品做我的工作,我的问题很简单。只是换个换乘地点而已。我不知道第一个或最后一个内容中负责确定运输地点的是什么问题。

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>

就是这样

最佳答案

来自 appendChild 文档:

Adds the node newChild to the end of the list of children of this node.

所以它按照预期将其添加到末尾。

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

ndListFirstFile.item(0).insertBefore(nodeArea, ndListFirstFile.item(0).getFirstChild());

关于java - 使用 JAVA 将标签从 xml 文件导入到另一个文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59310182/

相关文章:

c# - 将 XML 解析为 Treeview 列表

PHP DOMDocument getElementsByTagName 返回不完整的元素?

java - Facebook SDK - 登录后没有任何反应

java - 使用 Jdom 读取 xml 文件

java - 应用程序配置文件

javascript - 使用 Javascript 计算元素数量

javascript - 使用函数 getElementsByTagName 获取具有动态 ID 的元素的问题 - NightwatchJS

java - 为 Spring Boot 微服务设置集成测试

java - Android到服务器的AES加密/解密

java - 从 elasticsearch 索引或更新查询返回特定字段