为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

婷婷同学_ 2022-07-06 09:41:07
如何将标签“mohamed”从第一个文件导入到所有标签 ahmed 在第二个文件中<?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")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);
查看完整描述

2 回答

?
当年话下

TA贡献1890条经验 获得超9个赞

试试这个


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

}


查看完整回答
反对 回复 2022-07-06
?
慕哥9229398

TA贡献1877条经验 获得超6个赞

我想要这个结果


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

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


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



查看完整回答
反对 回复 2022-07-06
  • 2 回答
  • 0 关注
  • 104 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号