Issues with XMLParser

MaxpilotMaxpilot Posts: 4Member

Hi,
wrote a little script for parsing an xml-file to make a list with nested dictionaries and lists of the form:
[{"cdata": string, "childs": list, "name": string}] (childs listed under the "childs" - key).

extends Node

var mainList = []
var xp = XMLParser.new()
var err



func _init():

    var art
    var subdict

    xp.open("res://test2.xml")

    while xp.read() == 0:
        art = xp.get_node_type()
        print(art)

        if art == XMLParser.NODE_ELEMENT:
            subdict = {}
            subdict["name"] = xp.get_node_name()
            subdict["cdata"] = ""
            subdict["childs"] = []
            mainList.append(subdict)
            accu(subdict)
    print(mainList)



func accu(dict):

    var art
    var subdict

    while xp.read() == 0:
        art = xp.get_node_type()
        print(art)
        if art == XMLParser.NODE_ELEMENT:
            subdict = {}
            subdict["name"] = xp.get_node_name()
            subdict["cdata"] = ""
            subdict["childs"] = []
            dict["childs"].append(subdict)
            accu(subdict)
        elif art == XMLParser.NODE_ELEMENT_END:
            return
        elif art == XMLParser.NODE_TEXT:
            var untrimmed = xp.get_node_data()
            var trimmed = untrimmed.strip_edges()

            if ! trimmed.empty():
                dict["cdata"] = dict["cdata"] + untrimmed

Not complete, no error checking, but it fit my needs. From my point of view there are some issues with text nodes (if I'm not doing someting completely wrong): In UTF-8 the BOM is recognized as NODE_TEXT (minor problem), formating whitespace is recognized as text (to handle this see last lines of code), and last, and thats my question, how can I read data of a CDATA_NODE?

Answers

  • barab4barab4 Posts: 1Member

    Hi, I'm late to the party but I thank you for your code snippet! it helped me.
    One thing I noted is missing:
    In line 45. you only want to go one level deeper if !xp.is_empty()

Sign In or Register to comment.