如何将标签从第一个文件导入到第二个文件中的所有标签 [英] How to import a Tag from the first file to all Tag In the second file

查看:108
本文介绍了如何将标签从第一个文件导入到第二个文件中的所有标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何从第一个文件中导入标记为穆罕默德"的标记到第二个文件中的所有标记

How to import a Tag "mohamed" From the first file to all TAG ahmed In the second file

<?xml version="1.0" encoding="UTF-8"?>
<map
      xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9        http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
<!-- created with Free Online Sitemap Generator www.xml-sitemaps.com -->


<mohamed>stackover flow</mohamed>
</map>

我的Xml-2文件是

<?xml version="1.0" encoding="UTF-8"?>
<map
      xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
            http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
<!-- created with Free Online Sitemap Generator www.xml-sitemaps.com -->

<ahmed></ahmed>
<ahmed></ahmed>
<ahmed></ahmed>

</map>

在此处输入代码

有了这些片段,我可以将TagName("mohamed")导入TagName("ahmed")仅第一 我想将其导入第二个文件中的每个TagName("ahmed")

With these pieces i can import TagName("mohamed") to the TagName("ahmed") First only I want to import it into every TagName("ahmed") In the second file

public static void t (int f ,  int g,String z) {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = null;
    Document doc = null;
    Document doc2 = null;
String a = "C:\\Users\\chirap\\Desktop\\Startimes\\C.txt" ;
String  c ;
    try {
            db = dbf.newDocumentBuilder();
            doc = db.parse(new File(a));

            doc2 = db.parse(new File("C:\\Users\\chirap\\Desktop\\Startimes\\A (1).txt"));
            NodeList ndListFirstFile = doc.getElementsByTagName("ahmed");

            Node nodeArea = doc.importNode(doc2.getElementsByTagName("mohamed").item(0), true);


        NodeList nList2 = doc2.getElementsByTagName("mohamed");

            for (int i = f; i <g; i++) {
                c = i+"" ;
               doc2 = db.parse(new File("C:\\Users\\chirap\\Desktop\\Startimes\\A ("+c+").txt"));

            for (int temp = 0; temp < nList2.getLength(); temp++) {

                nodeArea = doc.importNode(doc2.getElementsByTagName("mohamed").item(temp), true);
                   ndListFirstFile.item(0).appendChild(nodeArea);

现在的结果:

<?xml version="1.0" encoding="UTF-8"?>
<map
      xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
            http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
<!-- created with Free Online Sitemap Generator www.xml-sitemaps.com -->

<ahmed><mohamed>stackover flow</mohamed></ahmed>
<ahmed></ahmed>
<ahmed></ahmed>

</map>

我想要这个结果:

<?xml version="1.0" encoding="UTF-8"?>
<map
      xmlns="http://www.sitemaps.org/schemas/sitemap/0.9"
      xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="http://www.sitemaps.org/schemas/sitemap/0.9
            http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd">
<!-- created with Free Online Sitemap Generator www.xml-sitemaps.com -->

    <ahmed><mohamed>stackover flow</mohamed></ahmed>
    <ahmed><mohamed>stackover flow</mohamed></ahmed>
    <ahmed><mohamed>stackover flow</mohamed></ahmed>

    </map>

推荐答案

尝试与此

try {
     File inputOne = new File("first.xml");
     File inputTwo = new File("second.xml");

     DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
     DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
     Document docOne = dBuilder.parse(inputOne);
     Document docTwo = dBuilder.parse(inputTwo);
     NodeList nodeListAhmed = docTwo.getElementsByTagName("ahmed");

     for (int i = 0; i < nodeListAhmed.getLength(); i++) {
         Node nodeMohamed = docTwo.importNode(docOne.getElementsByTagName("mohamed").item(0), true);
         nodeListAhmed.item(i).appendChild(nodeMohamed);
     }

     DOMSource source = new DOMSource(docTwo);
     TransformerFactory transformerFactory = TransformerFactory.newInstance();
     Transformer transformer = transformerFactory.newTransformer();
     StreamResult result = new StreamResult("output.xml");
     transformer.transform(source, result);

} catch (Exception e) {
     e.printStackTrace();
}

这篇关于如何将标签从第一个文件导入到第二个文件中的所有标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆