0

I have a strings with parmeters, which are the result of my software.

For example, System.out.println("The number is:" + count) is one of the results. The parameters can be any type: int, double or Date type.

Now I want to put this string (with the parameter count, for example) in a vector or any other data structure, and create XML, which would load later. There is any way to do that?

Thanks.

1

1 Answer 1

0

You can create your own java bean that will hold the message, the parameter type, and the value (and probably the parameter name if you need). This custom object can be de/serialized from/to XML by using XStream API it is really simple:

Person joe = new Person("Joe", "Walnes");
joe.setPhone(new PhoneNumber(123, "1234-456"));
joe.setFax(new PhoneNumber(123, "9999-999"));

XStream xstream = new XStream();

String xml = xstream.toXML(joe);

will result in following xml:

<person>
  <firstname>Joe</firstname>
  <lastname>Walnes</lastname>
  <phone>
    <code>123</code>
    <number>1234-456</number>
  </phone>
  <fax>
    <code>123</code>
    <number>9999-999</number>
  </fax>
</person>

to reconstruct the object you just:

Person newJoe = (Person)xstream.fromXML(xml);

Alternatively you can prepare your own (simple in your case) de/serialization utility based on java standard packages (SAX). Example

your xml can be something like:

<strings>
    <mystring>
        <message>
            "The number is:"
        </message>
        <paramType>
            int
        </paramType>
        <paramVal>
            42
        </paramVal>
    </mystring>
    ...
    <mystring>
        <message>
            "The date is:"
        </message>
        <paramType>
            Date
        </paramType>
        <paramVal>
            07/04/2012
        </paramVal>
    </mystring>
</strings>

Good luck!

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.