在 JAVA 中导入标签文本内容而不是完整标签

import Tag Text Content Instead of full Tag in JAVA

我需要一个简单的 Java 代码修改以仅导入 XML 元素的文本内容而不是整个元素。

如何使用 getElementsByTagName() 仅获取标签名称的值?

这是我正在阅读的名为 A (1) 的 XML 文件。xml:

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

    <map>I want to import this content only</map>
    </site>

请注意,该文件包含一个 <map> 元素,距末尾一行。

这是我正在写入的 XML 文件的内容:

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

    <site>I do not want to lose this content </site>
</tap>

我想提取 TagName("map") 的文本值,而不是标签本身,并将该文本写入如上所示的文件。这是我的代码:

package startimes;

import java.io.File;
import java.io.IOException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.*;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
import java.io.*;

public class Startimes {

    public static void main(String argv[]) {
        int r = 1;
        int l = 1;
        int s = 1;
        String nom;
        for (int i = 0; i < s; i++) {
            nom = "B (" + (i + 1);
            t(r, r + l, nom);
            r = r + l;
        }
    }

    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.xml";
        String c;
        try {
            db = dbf.newDocumentBuilder();
            doc = db.parse(new File(a));
            doc2 = db.parse(new File("C:\Users\chirap\Desktop\Startimes\A (1).xml"));
            NodeList ndListFirstFile = doc.getElementsByTagName("site");
            Node nodeArea = doc.importNode(doc2.getElementsByTagName("map").item(0), true);
            NodeList nList2 = doc2.getElementsByTagName("map");
            for (int i = f; i < g; i++) {
                c = i + "";
                doc2 = db.parse(new File("C:\Users\chirap\Desktop\Startimes\A (" + c + ").xml"));
                for (int temp = 0; temp < nList2.getLength(); temp++) {
                    nodeArea = doc.importNode(doc2.getElementsByTagName("map").item(temp), true);
                    ndListFirstFile.item(0).appendChild(nodeArea);
                }
            }
            TransformerFactory tFactory = TransformerFactory.newInstance();
            Transformer transformer = tFactory.newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            DOMSource source = new DOMSource(doc);
            StreamResult result = new StreamResult(new StringWriter());
            transformer.transform(source, result);
            Writer output = new BufferedWriter(new FileWriter("C:\Users\chirap\Desktop\Startimes\" + z + ").xml"));
            String xmlOutput = result.getWriter().toString();
            output.write(xmlOutput);
            output.close();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TransformerException e) {
            e.printStackTrace();
        }
    }
}

我想要这个结果:

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

<site>I do not want to lose this content 
I want to import this content only</site>

</tap>

请注意,最后一行应该只包含文本,但是当我 运行 我的代码看起来像这样:

<map>I want to import this content only</map>

我想排除那些 <map> 标签。提前谢谢你

ndListFirstFile.item(0).appendChild(nodeArea); 代码将 <map> 子元素及其值附加到 <site> 父元素下。输出如下,

<site .......................>
    <map>I want to import this content only</map>
</site>

仅将内容(</map> 元素的值)设置为 <site> 元素,尝试使用此

ndListFirstFile.item(0).setTextContent(nodeArea.getTextContent());

输出

<site .......................>I want to import this content only</site>

试试这个,

for (int temp = 0; temp < nList2.getLength(); temp++) {
    ndListFirstFile.item(0).setTextContent(ndListFirstFile.item(0).getTextContent()+nodeArea.getTextContent());
}