I'm looking for an easier way to generate and modify an XML file in Java. What I want to do is something like this:
<?xml version = "1"?>
<exchange>
<name> Exchange_1 </name>
<queue> Queue_1 </queue>
<queue> Queue_2 </queue>
</exchange>
<exchange>
<name> Exchange_2 </name>
<queue> Queue_3 </queue>
<queue> Queue_4 </queue>
</exchange>
It doesnt have to conform to any standard because this file is written/read by the same program. What I need to is to be able to modify the elements. As you can see, this is an exchange-queue description. So when I add a new exchange, I want to create a new entry, and then when I bind queues to that exchange they need to go under the tag for that exchange.
I开发者_JAVA技巧 want to able to read this file later and see what exchanges exist and what queues are bound to them. But I also want to be able to remove queues and exchanges. So for example when I remove a queue, I only remove that <queue>
element, but when I remove an exchange, I want to remove the whole <exchange>
with its enclosing <queue>
s.
Is there an easier/efficient way of doing something like this?
Thank you.
Use JAXB as Sahil Muthoo says. Yo can marshall and unmarshall the XML to java classes, and then work with the java classes.
JAXB examples
Yes, there is an easier way: Don't use XML.
Instead, use a Queue of Serializable objects and serialize the queue to disk for later deserialization.
IMHO, XML really, really sucks.
精彩评论