Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Writing an XML code beautifier  (Read 3736 times)

0 Members and 1 Guest are viewing this topic.

Smigs

    Topic Starter


    Starter

    • Experience: Experienced
    • OS: Linux variant
    Writing an XML code beautifier
    « on: November 23, 2011, 08:06:03 PM »
    Here's the idea....

    Given an input string containing XML data, without any indentation, output a string of XML with proper indentation.

    Example:

    Input string:
    Code: [Select]
    <collection><book id="1"><title>Charlie and the Chocolate Factory</title></book><book id="2"><title>C, How to Program</title</book></collection>
    Output string:
    Code: [Select]
    <collection>
        <book id="1">
            <title>Charlie and the Chocolate Factory</title>
        </book>
        <book id="2">
            <title>C, How to Program</title
        </book>
    </collection>

    I've never tried to tackle anything like this before, so any help is appreciated. I just need a starting point.
    Two atoms are walking down a street. One atom says to the other, "S***, I lost an electron!".
    The other says, "Are you sure?"
    The first one replies "Yeah, I'm positive".

    Salmon Trout

    • Guest
    Re: Writing an XML code beautifier
    « Reply #1 on: November 24, 2011, 12:06:53 AM »
    Think about how you would do it by hand and mimic that process in your code. Start with an indent of zero and add to it every time you encounter an opening tag and subtract every time you encounter a closing one.


    Smigs

      Topic Starter


      Starter

      • Experience: Experienced
      • OS: Linux variant
      Re: Writing an XML code beautifier
      « Reply #2 on: November 24, 2011, 12:37:32 AM »
      Ya, that's what I have kind of started to do. Thanks for the reply  :)
      Two atoms are walking down a street. One atom says to the other, "S***, I lost an electron!".
      The other says, "Are you sure?"
      The first one replies "Yeah, I'm positive".

      arrunsay897



        Newbie

        • Experience: Beginner
        • OS: Unknown
        Re: Writing an XML code beautifier
        « Reply #3 on: November 25, 2011, 06:26:57 AM »
        I'm not that good with xml myself but i can get by using an xml editor, try using Liquid XML Editor as it has syntax highlighting, auto code completion, xml validation etc etc