链接 XML 和 XSD 使用 Java。

huangapple 未分类评论51阅读模式
标题翻译

Link XML and XSD using java

问题

public void exportToXML() {
    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder dBuilder;

    try {

        dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.newDocument();
        doc.setXmlStandalone(true);

        Element fileElement = doc.createElement("file");
        fileElement.setAttribute("xmlns", "http://my_namespace");
        fileElement.setAttribute("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
        fileElement.setAttribute("xsi:schemaLocation", "http://my_namespace file.xsd");
        doc.appendChild(fileElement);

        Element mainRootElement = doc.createElement("MainRootElement");
        fileElement.appendChild(mainRootElement);

        for(int i = 0; i < tipoDadosParaExportar.length; i++) {
            mainRootElement.appendChild(criarFilhos(doc, tipoDadosParaExportar[i]));
        }
        Transformer tr = TransformerFactory.newInstance().newTransformer();
        tr.transform(new DOMSource(doc),
                new StreamResult(new FileOutputStream(filename)));

    } catch (Exception e) {
        e.printStackTrace();
    }
}
英文翻译

i'm trying to write the header for an xml file so it would be something like this:

&lt;file xmlns=&quot;http://my_namespace&quot;
xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
xsi:schemaLocation=&quot;http://my_namespace file.xsd&quot;&gt;

however, I can't seem to find how to do it using the Document class in java. This is what I have:

public void exportToXML() {
        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder;

        try {

            dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.newDocument();
            doc.setXmlStandalone(true);
            doc.createTextNode(&quot;&lt;file xmlns=\&quot;http://my_namespace&quot;\n&quot; +
                    &quot;xmlns:xsi=\&quot;http://www.w3.org/2001/XMLSchema-instance\&quot;\n&quot; +
                    &quot;xsi:schemaLocation=\&quot;http://my_namespace file.xsd\&quot;&gt;&quot;);
            Element mainRootElement = doc.createElement(&quot;MainRootElement&quot;);
            doc.appendChild(mainRootElement);

            for(int i = 0; i &lt; tipoDadosParaExportar.length; i++) {
                mainRootElement.appendChild(criarFilhos(doc, tipoDadosParaExportar[i]));
            }
            Transformer tr = TransformerFactory.newInstance().newTransformer();
            tr.transform(new DOMSource(doc),
                    new StreamResult(new FileOutputStream(filename)));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

I tried writing it on the file using the createTextNode but it didn't work either, it only writes the version before showing the elements.

PrintStartXMLFile

Would appreciate if you could help me. Have a nice day

答案1

得分: 0

你的createTextNode()方法只适用于创建文本节点,不适用于创建元素。你需要使用createElement()来实现这一点。如果你正在通过构建树来完成此操作,那么你需要构建节点,不能编写词法标记。

我不确定MainRootElement应该是什么;你只提供了你期望输出的片段,所以很难说清楚。

创建一个DOM树,然后对其进行序列化是构建XML文件的一种相当费力的方式。使用诸如XMLEventWriter之类的工具会更简单。但老实说,我对所有现有的方法都感到沮丧,所以作为Saxon 10的一部分,我编写了一个新的库来实现这一目的。它简单地被称为“Push”,大致如下:

Processor proc = new Processor();
Serializer serializer = proc.newSerializer(new File(fileName));
Push push = proc.newPush(serializer);
Document doc = push.document(true);
doc.setDefaultNamespace("http://my_namespace");
Element root = doc.element("root")
  .attribute(new QName("xsi", "http://www.w3.org/2001/XMLSchema-instance", "schemaLocation"), 
             "http://my_namespace file.xsd");
doc.close();
英文翻译

Your createTextNode() method is only suitable for creating text nodes, it's not suitable for creating elements. You need to use createElement() for this. If you're doing this by building a tree, then you need to build nodes, you can't write lexical markup.

I'm not sure what MainRootElement is supposed to be; you've only given a fragment of your desired output so it's hard to tell.

Creating a DOM tree and then serializing it is a pretty laborious way of constructing an XML file. Using something like an XMLEventWriter is easier. But to be honest, I got frustrated by all the existing approaches and wrote a new library for the purpose as part of Saxon 10. It's called simply "Push", and looks something like this:

Processor proc = new Processor();
Serializer serializer = proc.newSerializer(new File(fileName));
Push push = proc.newPush(serializer);
Document doc = push.document(true);
doc.setDefaultNamespace(&quot;http://my_namespace&quot;);
Element root = doc.element(&quot;root&quot;)
  .attribute(new QName(&quot;xsi&quot;, &quot;http://www.w3.org/2001/XMLSchema-instance&quot;, &quot;schemaLocation&quot;), 
             &quot;http://my_namespace file.xsd&quot;);
doc.close();

huangapple
  • 本文由 发表于 2020年5月30日 14:39:42
  • 转载请务必保留本文链接:https://java.coder-hub.com/62098778.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定