简体   繁体   English

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

[英]Import Tag from xml file to another using JAVA

I do my work with this piece and my problem is simple.我用这件作品做我的工作,我的问题很简单。 Just changing the place of transfer.就是换个转运地。 I did not know what is the matter responsible for determining the place of transportation in the first or last content.我不知道在第一个或最后一个内容中负责确定运输地点的事项是什么。

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);
        }  
    } 

This is done from two files, and it works well, but the place of transferring the tag is at the end of the content.这是从两个文件完成的,效果很好,但传输标签的位置在内容的末尾。 I want it at the beginning of the content我想要它在内容的开头

<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>

That's it就是这样

From the appendChild docs:appendChild文档:

Adds the node newChild to the end of the list of children of this node.将节点 newChild 添加到此节点的子节点列表的末尾。

So it is adding it to the end as expected.所以它按预期将它添加到最后。

To insert it before any other element on that node, you can try:要在该节点上的任何其他元素之前插入它,您可以尝试:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM