使用java将XML转换为CSV,标记有冒号问题

qojgxg4l  于 2023-01-22  发布在  Java
关注(0)|答案(1)|浏览(150)

我无法使用带冒号的namespace-tag显示xml标记的值。
当标签中没有冒号的时候,代码可以正常工作,但是当我想显示一个包含冒号的标签时,程序不会抛出任何错误,只是不显示任何值。
下面是XML:

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

XSL:

<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet 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" version="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");
            
        }
    }

代码确实简化了,以找到问题的原因,但我无法弄清楚。如果我将删除lst:从xml中的标签和从xsl中,它可以工作,只要有冒号,程序不显示任何值.但我们收到的XML包含大量的标签与冒号,所以我会解决这个问题.
如果你知道问题出在哪里,请告诉我:)
预期产出:
主题Java

4zcjmb1e

4zcjmb1e1#

试试这个

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");

    }

相关问题