10 Replies Latest reply on Mar 15, 2007 9:59 AM by quinibi

    ParseString()

    quinibi
      Hello Everybody,

      I'm trying to open a XML document with Parsestring(), but in my documents it says that I have to put gParserObject.parseString(member("XMLtext")), but as it isn't a member I put gParserObject.parseString("C:\Macro\trax.xml") and it seems that it is not correct. Does anybody know how can I open the XML document from a location. And then I would like to look for a specific data and generate a text file with it, is it possible?

      ---
      Fernando
        • 1. Re: ParseString()
          Level 7
          Before you can use the xtra's parseString() method you need to create a
          valid instance of it (it's not clear that you're doing this).
          If you want to load XML from a known location you could use parseURL(),
          even from the local file system. However, it is asynchronous, so you
          need to loop on a frame checking for doneParsing()
          Once the document is parsed you can use makePropList() method to create
          a list for searching
          • 2. Re: ParseString()
            quinibi Level 1
            The exactly code I use is:

            on beginSprite me
            global gParserObject
            gParserObject = new(xtra "xmlparser")
            errCode = gParserObject.parseURL("C:\trax.xml")
            errString = gParserObject.getError()
            if voidP(errString) then
            parsedList = gParserObject.makeList()
            else
            alert "Sorry, there was an Error" && errString
            exit
            end if
            put gParserObject.child[1].name
            end

            But with the las put I get:

            Script Error: Handler not found in object
            put gParserObject.child[1].name
            #getPropRef
            • 3. Re: ParseString()
              johnAq Level 1
              Sean suggests using parseURL() on a local file which I have not done, alternatively you could use fileio to read the file's content into a variable and then use parseString on that data

              hth

              johnAq
              • 4. Re: ParseString()
                quinibi Level 1
                I have tried, but I got an error since I think I can only use the finction openFile() for text files. This is the code I have used:
                variable = new xtra("fileio")
                variable.openfile("C:\trax.xml")
                global gParserObject
                gParserObject = new(xtra "xmlparser")
                errCode = gParserObject.parsestring(member("variable"))
                Did you mean this? or if not can you show me an example? Thanks
                • 5. Re: ParseString()
                  quinibi Level 1
                  Actually the code I use is:

                  variable = new xtra("fileio")
                  variable.openfile("D:\Joaquin\aaooaiea.xml",0)
                  global gParserObject
                  gParserObject = new(xtra "xmlparser")
                  errCode = gParserObject.parsestring(member("variable"))
                  errString = gParserObject.getError()
                  if voidP(errString) then
                  parsedList = gParserObject.makeList()
                  else
                  alert "Sorry, there was an Error" && errString
                  exit
                  end if

                  But the error is:

                  Script error: String expected
                  errCode = gParserObject.parsestring(member("variable"))
                  <Void>
                  • 6. Re: ParseString()
                    Level 7
                    Like I suggested in my previous mail, you never check doneParsing()
                    before trying to turn your XML into a list. That is perhaps why it isn't
                    working for you.
                    Since parseURL() is asynchronous and you want to be doing this from
                    #beginSprite, John's suggestion of reading in the XML and using
                    parseString() is sensible. Below is a handler named parseXML that you
                    should place in a movie script and call from your beginSprite handler,
                    passing it the filename of the document you want to parse.
                    --
                    on beginSprite me
                    lParsedXML = parseXML("C:\trax.xml")
                    put lParsedXML.child[1].name
                    end

                    -- #movie script handler
                    -- implementing error checks is left as an exercise
                    -- so as not to muddy core functionality
                    on parseXML aFilename
                    fio = xtra("FileIO").new()
                    -- check instance
                    fio.openFile(aFilename, 1)
                    -- check for error
                    tString = fio.readFile()
                    -- check for error
                    fio.closeFile()
                    fio = VOID

                    parser = xtra("XMLParser").new()
                    tResult = parser.parseString(tString)
                    if voidP(tResult) then
                    tList = parser.makePropList()
                    else
                    -- error with parsing
                    tList = [parser.getError()]
                    end if
                    parser = VOID

                    return tList
                    end
                    • 7. Re: ParseString()
                      quinibi Level 1
                      That works, thanks.
                      But the only thing is that I try to put the script in a text window, and
                      put gParserObject.child[1].name
                      should type in the text box the name, but it doesn't put anything, even I am testing with the XML document of the manual, so I mean it is not problem of the XML...
                      Another question, now that I have parsed the XML how can I look for a specific attrbute or value in the XML document and get it to put in a text file or compare with another value?
                      Thank you very much
                      • 8. Re: ParseString()
                        quinibi Level 1
                        quote:

                        Originally posted by: quinibi
                        That works, thanks.
                        But the only thing is that I try to put the script in a text window, and
                        put gParserObject.child[1].name
                        should type in the text box the name, but it doesn't put anything, even I am testing with the XML document of the manual, so I mean it is not problem of the XML...
                        Another question, now that I have parsed the XML how can I look for a specific attrbute or value in the XML document and get it to put in a text file or compare with another value?
                        I can do this with the code:
                        --Fill xml
                        --First look for an empty field
                        lParsedXML = parseXML("D:\Joaquin\database.xml")

                        objFileio=new xtra("fileio")
                        objFileio.openfile("C:\trax\id.txt",1)
                        objFileio.readfile()

                        if(lParsedXML.child[1].child[1].chardata = "id=0") then
                        lParsedXML.child[1].child[1].chardata=objFileio --BUT IN THiS LINE I CANNOT PUT IN THE FIELD THE DATA OF THE TEXT FILE, I DON´T KNOW WHY? MAYBE I SHOULD NOT USE =??
                        objFileio.closefile()

                        Thank you very much


                        • 9. Re: ParseString()
                          Level 7
                          > But the only thing is that I try to put the script in a text window, and
                          > put gParserObject.child[1].name
                          > should type in the text box the name, but it doesn't put anything, even I am
                          > testing with the XML document of the manual, so I mean it is not problem of the
                          > XML...

                          I don't understand what you're saying here.

                          > Another question, now that I have parsed the XML how can I look for a specific
                          > attrbute or value in the XML document and get it to put in a text file or
                          > compare with another value?

                          You have the XML as a list, now you need to walk the list looking for
                          whatever.

                          > I can do this with the code:
                          > --Fill xml
                          > --First look for an empty field
                          > lParsedXML = parseXML("D:\Joaquin\database.xml")
                          >
                          > objFileio=new xtra("fileio")
                          > objFileio.openfile("C:\trax\id.txt",1)
                          > objFileio.readfile()
                          >
                          > if(lParsedXML.child[1].child[1].chardata = "id=0") then
                          > lParsedXML.child[1].child[1].chardata=objFileio --BUT IN THiS LINE I
                          > CANNOT PUT IN THE FIELD THE DATA OF THE TEXT FILE, I DON´T KNOW WHY? MAYBE I
                          > SHOULD NOT USE =??

                          And I'm not sure what you're trying to achieve with this. Are you
                          wanting to take the chardata at a particular node and write it out to disk?
                          • 10. Re: ParseString()
                            quinibi Level 1
                            I would like to write here lParsedXML.child[1].child[1].chardata, in this node the content of the text file which is in ObjFileio