How to write a xml file?

How to write a xml file?

·

2 min read

Hi everyone, in this short intro into what is xml files and write a xml file. XML stands for Extensible Markup Language it is a markup language used to stored and transfer data. Xml is similar html where tags is used identify the data. In html tags are used to display and format data. We will be using VS code with a extension called Red hat and Live Server.

Let's first create a file named Music.xml, in the top of the page we have to defined which version xml to use version "1.0" or "1.1", and defined which Unicode. This is called the prolog.

<?xml version="1.0" encoding="UTF-8"?>

Xml has a similar tree structure to html. First come the root which is parent element, next come the child Element and Attributes which are siding. Xml syntax are straight forward and easy to under Their are no predefine tags in xml instead you are able to name your tags. The syntax for tags are they are case sensitive, tags cannot space instead use underscore "_", and tags cannot start with a number or special character. To add a tag we are going to less than "<" name our tag then add greater than ">". In order to close the we are going less than "<" then a "/", next name of your tag then a greater than ">".

Music.PNG

In the Vscode if you left click the Musci.xml file. you should see this:

Screenshot (98).png

After clicking Live Server then a few seconds and its going to open our browser:

localhosted.png

Let's organize our data, for this purpose lets organize by the year the artist release their album. We are going to have years as our tags. Also we can add attribute to year tag.

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Music>
    <Year_1986 id="year1986">

    </Year_1986>

    <Year_1979 id="year1979">

    </Year_1979>

</Music>

Within our year tag we are going to nest other tags. Now add the artist tag under the year tag. Then within the artist tag lets add the album tag.

    <Year_1979 id="year1979">
        <Artist>

            <Album>

            </Album>

        </Artist>
    </Year_1979>

Now its time to add all the data into our file:

<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Music>
    <Year_1986 id="year1986">
        <Artist>
            Bon Jovi
            <Album>
                Slippery When Wet
            </Album>
        </Artist>
        <Artist>
            Dirty Work
            <Album>
                The Rolling Stones
            </Album>
        </Artist>
    </Year_1986>

    <Year_1979 id="year1979">
        <Artist>
            AC/DC
            <Album>
                Highway to Hell
            </Album>
        </Artist>
        <Artist>
            Pink Floyd
            <Album>The Wall</Album>
        </Artist>
    </Year_1979>

</Music>

Our browser should have a tree of our data:

tree.PNG

The final code can be found in github.

Source:

cs.uccs.edu/~cs526/jwsdp/docs/tutorial/doc/..

w3schools.com/xml/default.asp