如何生成emotionml格式?

时间:2017-05-28 13:04:46

标签: 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>

我创建了一个在java中获取此输出的方法。但我无法获得正确的输出。

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

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

             Element rootElement = emotiondoc.createElement("emotionml");
             emotiondoc.appendChild(rootElement);

             Element emotionEl = emotiondoc.createElement("emotion");
             Attr attrType = emotiondoc.createAttribute("name");
             attrType.setValue("angry");
             emotionEl.setAttributeNode(attrType);
             emotionEl.appendChild(
             emotiondoc.createTextNode("What was that all about?"));
             rootElement.appendChild(emotionEl);

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

    return emotiondoc;
}

方法的输出如下,

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

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

1 个答案:

答案 0 :(得分:0)

您正在设置emotion元素的属性 name 。但是您需要向category添加emotion元素,并将name属性添加到该元素。

修改

这是包含打印方法(which I copied from here)的完整代码,用于添加命名空间并将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>