Page 1 of 1

ExtractXMLMap

Posted: Fri May 01, 2015 7:13 pm
by bohne_68
Hallo,

i try to write and to read a MAP using the xml-features.

Code: Select all

  Procedure SaveSettings()
    Define FileName.s
    
    If MapSize(SettingsData()) > 0              
      If CreateXML(0) And InsertXMLMap(CreateXMLNode(RootXMLNode(0), "settings"), SettingsData())
        FileName = SettingsFileName() 
        SaveXML(0, FileName)
        FreeXML(0)          
      Else
        Debug XMLError(0)
      EndIf
    EndIf
  EndProcedure

  Procedure LoadSettings()
    Define FileName.s
    
    FileName = SettingsFileName()
    If FileSize(FileName) >= 0       
      If LoadXML(0, FileName) And XMLStatus(0) = #PB_XML_Success
        ExtractXMLMap(MainXMLNode(0), SettingsData())
        FreeXML(0)
      Else
        Debug XMLError(0)
      EndIf           
    EndIf
  EndProcedure

It is not possible to get the map back. Can somebody tell me, what is going wrong?

Thanks
Thomas

Re: ExtractXMLMap

Posted: Fri May 01, 2015 11:44 pm
by em_uk
ExtractXMLMap is for when working with a string as far as I can tell :

Code: Select all

Xml$ = "<map><element key=" + Chr(34) + "theKey" + Chr(34) + ">the value</element></map>"
  
  If ParseXML(0, Xml$) And XMLStatus(0) = #PB_XML_Success
    NewMap Test.s()
    ExtractXMLMap(MainXMLNode(0), Test())
    
    ForEach Test()
      Debug MapKey(Test()) + " -> " + Test()
    Next
  Else
    Debug XMLError(0)
  EndIf
When I was working with XML I had to use much more convoluted methods to load my XML from disk then parse the file myself. I'm not sure if things have gotten easier since then :)

Code: Select all

Procedure ImportDataFile_XML(*CurrentNode, CurrentSublevel.i)
  
  Protected NodeName.s, *ChildNode, AttributeName.s, FilenameString.s, PictureFileName.s, *Buffer
  Protected FileSize.i, PictureFileHandle.i, FileNamesCountLoop.i, FileNamePart.s
  Protected FileExtensionPart.s, Separator.s
  
  If XMLNodeType(*CurrentNode) = #PB_XML_Normal
    NodeName.s = GetXMLNodeName(*CurrentNode)
    
    ;;do stuff With the node data
    
    *ChildNode = ChildXMLNode(*CurrentNode)
    
    While *ChildNode <> 0
      
      ImportDataFile_XML(*ChildNode, CurrentSublevel + 1)
      *ChildNode = NextXMLNode(*ChildNode)
    Wend
    
  EndIf
  
EndProcedure

Procedure ImportDataFile()
  Protected.i XML
  Protected.q ImdbXMLFileSize.q
  Protected *Buffer, *Node
  ImdbXMLFile.i = ReadFile(#PB_Any, "level.xml")
  If ImdbXMLFile.i
    ImdbXMLFileSize.q = Lof(ImdbXMLFile.i)
    If ImdbXMLFileSize.q
      *Buffer = AllocateMemory(ImdbXMLFileSize.q)
      If *Buffer
        If ReadData(ImdbXMLFile, *Buffer, ImdbXMLFileSize.q) = ImdbXMLFileSize.q
          XML = CatchXML(#PB_Any, *Buffer, ImdbXMLFileSize.q)
          If XML
            *Node = MainXMLNode(XML)
            If *Node
              ImportDataFile_XML(*Node, 0)
            EndIf
            FreeXML(XML)

          EndIf
        EndIf
        FreeMemory(*Buffer)
      EndIf
      CloseFile(ImdbXMLFile.i)
    EndIf
  EndIf
EndProcedure

ImportDataFile()


Re: ExtractXMLMap

Posted: Sat May 02, 2015 10:40 am
by bohne_68
Thanks for your replay. I'am also tried the convertion to a string using the Method ComposeXML() with the same result.
Now I'am using the classical implementation, pasing the XML-tree manualy.

"It's not a bug, it's a feature" :mrgreen: