0

I search for an option to transform xml-files. Now i had found antlr4 and I ask me it is possible to write a Parser for xml-files? What I want is to read a xml-file in my program. Than I want to analyse the format of the file and than I want to restructure it. Is it possible? This example demonstrate my Question:

<?xml version="1.0" encoding="UTF-8"?>
<xmi:XMI xmi:version="2.1">
      <xmi:Documentation exporter="MalteFalk" exporterVersion="1.0"/>
    <person>
        <personelement type="aaa">
            <name value="Max" id="1"/>
            <lastname value="Hampelmann" id="2"/>
        </personelement>
        <personelement type="bbb">
            <name value="Hannes" id="3"/>
            <lastname value="Mustermann" id="4"/>
        </personelement>
    </person>
    <job>
        <jobelement type="ccc">
            <jobtitle value="barber"/>
            <salary value="50000"/>
        </jobelement>
        <jobelement type="ddd">
            <jobtitle value="doctor"/>
            <salary value="100000"/>
        </jobelement>
    </job>
    <relationship>
        <relation1>
            <source id="1"/>
            <target id="ddd"/>
        </relation1>
        <relation2>
            <source id="3"/>
            <source id="ccc"/>
        </relation2>
    </relationship>
</xmi:XMI>

Now I want to read in this file in my parser and restructure it into this xml-file:

<?xml version="1.0" encoding="UTF-8"?>
<xmi:XMI xmi:version="2.1">
  <xmi:Documentation exporter="XXX" exporterVersion="1.0"/>
    <taggedvalues>
      <tag type="Max">
        <firstname value="Max"/>
        <lastname value="Mustermann"/>
        <jobtitle value="baber"/>
        <salary value="50000"/>
      </tag>
      <tag type="Hannes">
        <firstname value="Hannes"/>
        <lastname value="Hampelmann"/>
        <jobtitle value="doctor"/>
        <salary value="100000"/>
      </tag>
    </taggedvalues>
</xmi:XMI>
MFY
  • 11
  • 2

1 Answers1

2

Yes, it is possible to write a parser for XML in ANTLR4.

No, you should not attempt to do so instead of using existing parsers or XSLT processors unless you are willing to devote hundreds, perhaps thousands, of hours to the endeavor to design, develop, and test the myriad of cases you're almost certainly not anticipating.

kjhughes
  • 106,133
  • 27
  • 181
  • 240
  • Thanks for your answer. But I need a parser that scanning a xml-file with no "standard" structure and it must be restructure in a standard structure so I can't use XSLT or? – MFY Jan 21 '15 at 15:43
  • XML by definition must be [***well-formed***](http://stackoverflow.com/a/25830482/290085). All XML parsers, and all XSLT processors, can read all XML, regardless of the schema to which it might adhere. *It simply must be well-formed.* If you're proposing to create a parser that reads non-well-formed markup, (1) it's not XML and (2) you'll have to *define* this new language precisely. I recommend that you clean up your input to make it well-formed, then use a standard XML parser or XSLT to further transform it to meet your needs. – kjhughes Jan 21 '15 at 16:03
  • Thanks. Yes it is XML and i think i use a standard XML parser like JDOM because i don't have a xsd-file... – MFY Jan 23 '15 at 06:44