Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/315.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/visual-studio/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何生成emotionml格式?_Java_Xml - Fatal编程技术网

Java 如何生成emotionml格式?

Java 如何生成emotionml格式?,java,xml,Java,Xml,我想生成以下格式的xml文件 <emotionml version="1.0" xmlns="http://www.w3.org/2009/10/emotionml"> <emotion><category name="angry"/> What was that all about? </emotion> </emotionml> 方法的输出如下所示 <?xml version="1.0" encoding="UTF-

我想生成以下格式的xml文件

<emotionml version="1.0" xmlns="http://www.w3.org/2009/10/emotionml">
<emotion><category name="angry"/>
What was that all about?
</emotion>
</emotionml>
方法的输出如下所示

    <?xml version="1.0" encoding="UTF-8" standalone="no"?>
    <emotionml>
        <emotion name="angry">
   What was that all about?.
    </emotion>
    </emotionml>

那是怎么回事?。

如何修改代码以获得此问题顶部所述的正确输出???

您正在设置
情绪
元素的属性名称。但是您需要将
类别
元素添加到
情感
中,并将name属性添加到该元素中

编辑:

下面是完整的代码,它使用print方法()添加名称空间并将
emotion
属性放入
category
。我使用了您在方法中定义的参数,我希望这与预期一致:

public class Test {
    public static void main(String[] args) {
        try {
            final Document document = printEmotionML("What was that all about?", "angry");
            printDocument(document, System.out);
        } catch (Throwable throwable) {
            throwable.printStackTrace();
        }
    }

    public static Document printEmotionML(String sentence, String emotion) {
        Document emotiondoc = null;
        try {

            DocumentBuilderFactory dbFactory =
                    DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder =
                    dbFactory.newDocumentBuilder();
            emotiondoc = dBuilder.newDocument();

            Element rootElement = emotiondoc.createElementNS("http://www.w3.org/2009/10/emotionml", "emotionml");
            rootElement.setAttribute("version", "1.0");
            emotiondoc.appendChild(rootElement);

            Element emotionEl = emotiondoc.createElement("emotion");
            rootElement.appendChild(emotionEl);

            final Element category = emotiondoc.createElement("category");
            emotionEl.appendChild(category);
            category.setAttribute("emotion", emotion);

            emotionEl.appendChild(emotiondoc.createTextNode(sentence));

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

        return emotiondoc;
    }

    public static void printDocument(Document doc, OutputStream out) throws IOException, TransformerException {
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

        transformer.transform(new DOMSource(doc),
                new StreamResult(new OutputStreamWriter(out, "UTF-8")));
    }

}
这将提供以下输出:

<emotionml version="1.0" xmlns="http://www.w3.org/2009/10/emotionml">
  <emotion>
    <category emotion="angry"/>What was that all about?</emotion>
</emotionml>

那是怎么回事?

谢谢您的更正。另外,请帮我更改输出的第一行。您能接受我的回答吗
<emotionml version="1.0" xmlns="http://www.w3.org/2009/10/emotionml">
  <emotion>
    <category emotion="angry"/>What was that all about?</emotion>
</emotionml>