This discussion is archived
3 Replies Latest reply: Nov 29, 2007 11:27 AM by 807603 RSS

Question about well formatted xml

807603 Newbie
Currently Being Moderated
Does anyone know how to make a xml stream to be well formatted?
For example, if I got a String like:
String a = "<person><name>Jack</name><age>16</age></person>";

And I need to convert it to be well formatted like

<person>
<name>Jack</name>
<age>16</age>
</person>

Any idea?
Thanks
  • 1. Re: Question about well formatted xml
    796254 Newbie
    Currently Being Moderated
    AllenZhao wrote:
    Does anyone know how to make a xml stream to be well formatted?
    "well formatted"? You're confusing this with "well formed".
    For example, if I got a String like:
    String a = "<person><name>Jack</name><age>16</age></person>";

    And I need to convert it to be well formatted like

    <person>
    <name>Jack</name>
    <age>16</age>
    </person>

    Any idea?
    Thanks
    Yeah, get something that will "pretty print" the XML for you. I believe JDOM can do it out of the box.


    %
  • 2. Re: Question about well formatted xml
    807603 Newbie
    Currently Being Moderated
    are you trying to format it while you are steaming it? that would be a bit difficult to get it how you want it, but this is possible:
    <person>
         <name>
             Jack
         </name>
         <age>
             16
         </age>
    </person>
    if you can read the entire string in before formatting, then its not that hard after you come up with an algorithm


    why are you doing this?
  • 3. Re: Question about well formatted xml
    807603 Newbie
    Currently Being Moderated
    The reason I'm doing that is to show the user clearly difference between two similar xml streams. I think I got it. Just use XMLOutputter's output method. Thanks everyone!