0

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 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.

0

2 Answers 2

2

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

Sign up to request clarification or add additional context in comments.

Comments

1

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.

4 Comments

Thanks Bohemian.But I need this written to a file because I want to share the structure between machines.
You can share the serialized object graph between machines. It just wont be XML. You could use JAXB to serialize to XML though.
@madu just to be clear, "serialize to disk" is a file, and it can be shared between machines, as long as they are both running with the same versions of your code. It is the simplest and best way to move java objects around. Warning here: If you change your queued object class after you've serialized it, it may not deserialize properly into the new version (depends on what the change is).
Thank you very much for the replies guys. I was not aware of JAXB and the writing Serialized objects to file.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.