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

时间:2019-02-28 16:14:36

标签: java xml netbeans getelementsbytagname

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

如何使用List.Number ="$('#number').html(new.Number);" 仅获取标签名称的值?

这是我正在读取的XML文件,名为 A(1).xml

getElementsByTagName()

请注意,文件末尾一行包含<?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> 元素。

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

<map>

我想提取TagName(“ map”)的文本值,而不是标签本身,然后将该文本写入上面显示的文件。这是我的代码:

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

我想要这个结果:

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> 标签。预先谢谢你

2 个答案:

答案 0 :(得分: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>

答案 1 :(得分:0)

尝试一下,

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