0

I would like to check whether all elements and attributes in a given XML file are spelled correctly. My idea would be to unmarshall the XML into some data structure using "encoding/xml" (perhaps a map?) and then explicitly check that every element and attribute is spelled correctly.

How could I unmarshall any valid XML file into a map-like data structure, in order to check that the XML provided is valid?

Here's a concrete example:

Let's say I have the following XML file, whereby the elements and attributes within are defined with a specific schema:

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

<servers version="1">
    <server>
        <model name="Cisco" type="modelA"></model>
        <serverName>Tokyo_VPN</serverName>
        <serverIP>127.0.0.1</serverIP>
    </server>
    <server>
        <model name="Dell" type="modelB"></model>
        <serverName>Moscow_VPN</serverName>
        <serverIP>127.0.0.2</serverIP>
    </server>
</servers>

From this, the valid elements and attributes are: servers, server, model, name, type, serverName, and serverIP.

Here is an XML which has misspelled elements:

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

<servers version="1">
    <router>
        <modell nambe="Cisco" typre="modelA"></model>
        <serverNameR>Tokyo_VPN</serverNameR>
        <serverIP>127.0.0.1</serverIP>
    </router>
</servers>

Note that this is valid XML, but doesn't follow my schema. Therefore, if I can figure out how to ummarshall any XML into something like a map, I could check that each key is valid.

EB2127
  • 1,788
  • 3
  • 22
  • 43
  • Does this answer your question? [How to check for errors in the input XML when parsing with Go?](https://stackoverflow.com/questions/63715329/how-to-check-for-errors-in-the-input-xml-when-parsing-with-go) – Shubham Srivastava Sep 07 '20 at 04:22
  • @ShubhamSrivastava It doesn't, so I asked a revised question. – EB2127 Sep 08 '20 at 14:43

0 Answers0