How to import a Tag from the first file to all Tag In the second file

孤街醉人 提交于 2019-12-11 18:45:33

问题


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>

My Xml-2 file is

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

enter code here

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

The result now:

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

I want this result:

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

回答1:


try with this

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



回答2:


I want this result

<?xml version="1.0" encoding="UTF-8"?>
<ahmed
      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 -->


stackover flow
</ahmed>

You can use .querySelector() to get the element having tagName "mohamed", store .parentElement of the element in a variable, store the .textContent of the matched elements in a variable, remove the matched element from parent element using .removeChild(), set the parent element .textContent to the stored variable

const xml = `<?xml version="1.0" encoding="UTF-8"?>
<ahmed
      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>
</ahmed>`;
const parser = new DOMParser();
const doc = parser.parseFromString(xml, "text/xml");
let el = doc.querySelector("mohamed");
let parentElement = el.parentElement;
let text = el.textContent;
parentElement.removeChild(el);
parentElement.textContent = text;
console.log(`<?xml version="1.0" encoding="UTF-8"?>${doc.documentElement.outerHTML}`);


来源:https://stackoverflow.com/questions/54854849/how-to-import-a-tag-from-the-first-file-to-all-tag-in-the-second-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!