首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何将标签从第一个文件导入到第二个文件中的所有标记

如何将标签从第一个文件导入到第二个文件中的所有标记
EN

Stack Overflow用户
提问于 2019-02-24 18:01:20
回答 2查看 83关注 0票数 0

如何将标签"mohamed“从第一个文件导入到第二个文件中的所有标记ahmed

代码语言:javascript
复制
<?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文件是

代码语言:javascript
复制
<?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(“TagName”)中。

代码语言:javascript
复制
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);

现在的结果是:

代码语言:javascript
复制
<?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>

我想要这个结果:

代码语言:javascript
复制
<?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>
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2019-03-05 18:08:26

试试这个

代码语言:javascript
复制
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();
}
票数 1
EN

Stack Overflow用户

发布于 2019-02-24 18:21:35

我想要这个结果

可以使用.querySelector()获取具有tagName "mohamed"的元素,将元素的.parentElement存储在变量中,将匹配元素的.textContent存储在变量中,使用.removeChild()从父元素中删除匹配的元素,将父元素.textContent设置为存储的变量。

代码语言:javascript
复制
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}`);

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/54854849

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档