作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
如何将第一个文件中的标签“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);
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();
}
关于java - 如何将第一个文件中的标签导入到第二个文件中的所有标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54854849/
我是一名优秀的程序员,十分优秀!