使用Java将XML转换为CSV,带有冒号问题的标签

时间:2018-11-26 17:05:35

标签: java xml csv tags colon

我无法使用命名空间-带有冒号的标记来显示xml标记的值。

当标签中没有冒号时,代码可以完美地工作,但是一旦我想显示一个包含冒号的标签,程序就不会抛出任何错误,只会显示任何值。

这是XML:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:lst="http://www.esa.int/safe/sentinel-1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform" xmlns:inv="http://www.w3schools.com/daco">
<lst:howto>
    <topic id="1">
        <nieco>

              <lst:title>Java</lst:title>



        </nieco>


    </topic>

</lst:howto>


</xsl:stylesheet>

XSL:

<?xml version="1.0"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                              xmlns:fo="http://www.w3.org/1999/XSL/Format" xmlns:lst="http://www.esa.int/safe/sentinel-1.0" >
<xsl:output method="text" omit-xml-declaration="yes" indent="no"/>
<xsl:template match="/">
topic
<xsl:for-each select="//nieco">

   <xsl:value-of select="lst:title"/>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>

和Java:

import java.io.File;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

import org.w3c.dom.Document;

    class XMLtoCsVConversion2 {

        public static void main(String args[]) throws Exception {
            File stylesheet = new File("C:/java/howto.xsl");
            File xmlSource = new File("C:/java/howto.xml");

            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse(xmlSource);

            StreamSource stylesource = new StreamSource(stylesheet);
            Transformer transformer = TransformerFactory.newInstance()
                    .newTransformer(stylesource);
            Source source = new DOMSource(document);
            Result outputTarget = new StreamResult(new File("xyz.csv"));
            transformer.transform(source, outputTarget);
            System.out.println("done");

        }
    }

代码确实简化了查找问题的原因,但是我无法弄清楚。 如果我从xml中的标签和xsl中删除lst:,它将起作用,一旦出现冒号,程序将不会显示任何值。 但是我们收到的XML包含大量带有冒号的标签,因此我将解决此问题。

如果您有任何疑问,请告诉我:)

谢谢

预期输出: 话题 Java

1 个答案:

答案 0 :(得分:0)

尝试一下;

    public static void main(String args[]) throws Exception {
        File stylesheet = new File("C:/java/howto.xsl");
        File xmlSource = new File("C:/java/howto.xml");

//        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//        DocumentBuilder builder = factory.newDocumentBuilder();
//        Document document = builder.parse(xmlSource);

        StreamSource stylesource = new StreamSource(stylesheet);
        Transformer transformer = TransformerFactory.newInstance()
                .newTransformer(stylesource);
        //Source source = new DOMSource(document);
        Source source = new StreamSource(xmlSource);
        Result outputTarget = new StreamResult(new File("C:/java/xyz.csv"));
        transformer.transform(source, outputTarget);
        System.out.println("done");

    }