0

I want to generate a xml file in the following format,

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

I created a method to get this output in java. But the I couldn't get the right output.

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;
}

The output of the method as follows,

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

How can I modify the code to get the right output mentioned in the top of this question??

ReB
  • 107
  • 10

1 Answers1

0

you are setting the attribute name of the emotion element. But you need to add a category element to emotion and add the name attribute to that element.

Edit:

here is the complete code with a print method (which I copied from here) adding the namespace and putting the emotion attribute to the category. I used the parameters you defined in the method, I hops this is as intendend:

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

}

This gives the following output:

<emotionml version="1.0" xmlns="http://www.w3.org/2009/10/emotionml">
  <emotion>
    <category emotion="angry"/>What was that all about?</emotion>
</emotionml>
P.J.Meisch
  • 18,013
  • 6
  • 50
  • 66