导入标记文本内容而不是JAVA中的完整标记

问题描述 投票:0回答:2

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

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

这是我正在阅读的XML文件名为A(1).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>标签。先感谢您

java xml netbeans getelementsbytagname
2个回答
0
投票

试试这个,

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

0
投票

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>
© www.soinside.com 2019 - 2024. All rights reserved.