0

I want to write an xml file using the following format:

<root>
    <date> 9:51 AM 10/10/2012 </date>
    <responseTime> 1.20</responseTime>
    <employee>
        <name> Mohammad</name>
    </employee>
    <employee>
        <name> Ali</name>
    </employee>
    <employee>
        <name> Mostafa</name>
    </employee>
    <employee>
        <name> Mahmoud</name>
    </employee>
</root>

Can I wrote it using DOM? or should I write it by hand?

(The problem in that the employee node is a sequence without a direct parent node to warp all employee elements without date and responseTime elements)

Muhammad Hewedy
  • 29,102
  • 44
  • 127
  • 219
  • 1
    "The problem in that the employee node is a sequence without a parent node" — What is `` if not a parent node? – Quentin Oct 10 '12 at 08:24
  • Have a sequence with date and responseTime followed by 1 or more instances of employee. 1 or more instances of employee can itself be a complex type. – Neil Oct 10 '12 at 08:50

2 Answers2

1

I don't see the problem with doing it with DOM.

Code:

public static void main(String[] args) throws ParserConfigurationException, IOException, TransformerException
{
    DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
    DocumentBuilder documentBuilder = builderFactory.newDocumentBuilder();
    Document document = documentBuilder.newDocument();
    Element root = document.createElement("root");
    document.appendChild(root);

    Element emp1 = document.createElement("employee");
    Element emp1name = document.createElement("name");
    emp1name.setTextContent("Mohammad");
    emp1.appendChild(emp1name);
    Element emp2 = document.createElement("employee");
    Element emp2name = document.createElement("name");
    emp2name.setTextContent("Ali");
    emp2.appendChild(emp2name);

    root.appendChild(emp1);
    root.appendChild(emp2);

    printDocument(document, System.out);
}

Output:

<root>
    <employee>
        <name>Mohammad</name>
    </employee>
    <employee>
        <name>Ali</name>
    </employee>
</root>

You can see the source code for printDocument in this SO Answer.

Full source code can be found here.

Community
  • 1
  • 1
RonK
  • 9,472
  • 8
  • 51
  • 87
  • The major problem I am talking about is to have this sequence with `responseTime` and `date` elements – Muhammad Hewedy Oct 10 '12 at 08:36
  • @Muhammad - I don't see the problem with the sequence - check this link, it will show you it works fine: http://ideone.com/bfTbl – RonK Oct 10 '12 at 08:47
  • Ohh, yes. But Can we have an XSD for that? I need to generate such output from some DTO java object to have this xml generated? http://stackoverflow.com/questions/12815335/how-to-write-an-xsd-for-a-list-with-no-wrapper-element – Muhammad Hewedy Oct 10 '12 at 08:50
  • That is a totally different question - this is the 1st time you mentioned an XSD - as far as I know, the answer is yes. You can define an XSD for that. – RonK Oct 10 '12 at 08:57
  • How to do so?, please follow my question – Muhammad Hewedy Oct 10 '12 at 08:59
  • I did this at first, please edit your answer so I can undo-downvote. Thanks. – Muhammad Hewedy Oct 11 '12 at 09:44
0

I think you can write that with DOM (the parent node for "employee" is "root"), however it would be nicer to wrap "employee" nodes with "employees" for example...

DrArt
  • 141
  • 1
  • 1
  • 6