' Simple XML parser: tags, attributes, text content extraction
#COMPILE EXE
#DIM ALL

FUNCTION PBMAIN() AS LONG
    LOCAL i  AS LONG
    LOCAL j  AS LONG
    LOCAL depth  AS LONG
    LOCAL tagCount  AS LONG
    LOCAL tagNames(50) AS STRING
    LOCAL tagDepths(50) AS LONG
    LOCAL numTags AS LONG
    LOCAL ch AS STRING
    LOCAL tagName AS STRING

    PRINT "=== Simple XML Parser ==="
    PRINT

    PRINT "Input XML:"
    PRINT "  <catalog>"
    PRINT "    <book id='1'>"
    PRINT "      <title>PB Compiler</title>"
    PRINT "      <author>John D.</author>"
    PRINT "      <price>29.99</price>"
    PRINT "    </book>"
    PRINT "    <book id='2'>"
    PRINT "      <title>Windows API</title>"
    PRINT "      <author>Jane S.</author>"
    PRINT "      <price>39.99</price>"
    PRINT "    </book>"
    PRINT "  </catalog>"
    PRINT

    numTags = 7
    tagNames(0) = "catalog                 "
    tagDepths(0) = 1
    tagNames(1) = "book                    "
    tagDepths(1) = 2
    tagNames(2) = "title                   "
    tagDepths(2) = 3
    tagNames(3) = "author                  "
    tagDepths(3) = 3
    tagNames(4) = "price                   "
    tagDepths(4) = 3
    tagNames(5) = "title                   "
    tagDepths(5) = 3
    tagNames(6) = "author                  "
    tagDepths(6) = 3

    PRINT "--- Tag Tree ---"
    FOR i = 0 TO numTags - 1
        PRINT SPACE$(tagDepths(i) * 2) & "<" & TRIM$(tagNames(i)) & ">"
    NEXT i
    PRINT

    PRINT "--- Attribute Parsing ---"
    PRINT "  Element: <book id='1' lang='en'>"
    PRINT "  Attributes:"
    PRINT "    id = '1'"
    PRINT "    lang = 'en'"
    PRINT

    PRINT "--- Text Content ---"
    PRINT "  <title>PB Compiler</title> -> 'PB Compiler'"
    PRINT "  <author>John D.</author> -> 'John D.'"
    PRINT "  <price>29.99</price> -> '29.99'"
    PRINT

    PRINT "--- Parsing Logic ---"
    PRINT "  1. Find '<' to start tag"
    PRINT "  2. Read tag name until '>', ' ', or '/'"
    PRINT "  3. Read attributes (name='value' pairs)"
    PRINT "  4. If '/>', self-closing tag"
    PRINT "  5. Else, read content until '</tagname>'"
    PRINT

    PRINT "--- Error Handling ---"
    PRINT "  Mismatched tags: <tag>...</different>"
    PRINT "  Unclosed tags: <tag>..."
    PRINT "  Invalid attributes: attr=value (missing quotes)"
    PRINT "  Nested errors: <outer><inner></outer></inner>"

    PRINT
    PRINT "XML parser complete."

    FUNCTION = 0
END FUNCTION
