2013-05-06 111 views
0

這個問題關於將XML數據從LiveCode堆棧寫入文件。 用戶指南的第6.7章討論了LiveCode提供的XML函數。我正在尋找示例來展示如何構建XML文件並將其寫入磁盤文件。如何構建和編寫XML文件?

http://support.runrev.com/tutorials/xmldemo.rev.gz是關於如何使用LiveCode的revNNN XML功能的教程堆棧。

它有一個例子

.... 
    local tDocID, tParentNode, tSubNode 

    -- get the document ID for the current XML tree 
    put fld "DocID" into tDocID 

    -- specify the root node for the XML tree 
    put "/employeeTable" into tParentNode 

    revAddXMLNode tDocID, tParentNode, "employee", "" 
    put the result into tSubNode 

    -- add the IDnum attribute to the newly created data record 
    revSetXMLAttribute tDocID, tSubNode, "IDnum", "1" 

    -- add the remaining data elements, checking for error after each addition 
    revAddXMLNode tDocID, tSubNode, "firstName", "Steve" 
    revAddXMLNode tDocID, tSubNode, "lastName", "Jobs" 
    revAddXMLNode tDocID, tSubNode, "roomNum", "001" 
    revAddXMLNode tDocID, tSubNode, "phoneExt", "345" 
    revAddXMLNode tDocID, tSubNode, "parkingSlot", 100 

結果

<?xml version="1.0"?> 
    <employeeTable> 

    <employee IDnum="1"> 
    <firstName>Steve</firstName> 
    <lastName>Jobs</lastName> 
    <roomNum>001</roomNum> 
    <phoneExt>345</phoneExt> 
    <parkingSlot>100</parkingSlot> 
    </employee> 

</employeeTable> 

是否有庫,這使得通過提供方便的功能編寫XML文本更容易讓我不需要跟蹤節點加入時嵌套結構?

喜歡的東西

startXML "theEmployees.xml" -- gives the file name 
startTag "employeetable" 
    startTag "employee" 
    addAttribute "IDnum", 1 
    startTag "firstName" 
     writeContent "Steve" 
    closeTag 
    -- or 
    writeNode "lastname", "Jobs" 
    writeNode "roomnum", "001" 
    -- .... 
    closeTag -- employee 
closeTag -- employeeTable 
closeXML 

這是比較容易寫這樣一對夫婦的功能,但問題是。是否已經建立了將XML文本寫入LiveCode文件的方法?

+0

這是由Mark Wieder(公共領域)提供的版本控制庫,它包含一些XML編寫過程。 http://revonline2.runrev.com/stack/686/libVersionControl – 2013-05-06 09:53:40

+0

在我的書「爲真正的初學者編寫LiveCode」中有一章關於創建和讀取XML文件。 – Mark 2013-06-15 11:19:34

回答

1

如果你只是想寫XML(而不是創建revXMLTrees),你可以編寫自己的函數。這個怎麼樣的首發:你可以

local _tags 
local _xml 
local _level 
local _tabs 

function q pText 
    return quote & pText & quote 
end q 

on startXML 
    put "<?xml version=" & q("1.0") & "?>" & return into _xml 
    put 0 into _level 
    put empty into _tabs 
    put empty into _tags 
end startXML 

on startTag pTag 
    put _tabs & "<" & pTag & ">" & return after _xml 
    add 1 to _level 
    put pTag into _tags[_level] 
    put tab after _tabs 
end startTag 

on closeTag 
    delete char 1 of _tabs 
    put _tabs & "</" & _tags[_level] & ">" & return after _xml 
    # Do a 'delete variable _tags[_level]' if you really want to clean the array as you go 
    subtract 1 from _level 
end closeTag 

on addAttribute pAttribute, pValue 
    # This should go into the last tag, but as we have "proper XML" so far we can backtrace two chars (">" and newline) 
    put space & pAttribute & "=" & q(pValue) before char -2 of _xml 
end addAttribute 

on writeContent pContent 
    put _tabs & pContent & return after _xml 
end writeContent 

on writeNode pNode, pValue 
    put _tabs & "<" & pNode & ">" & pValue & "</" & pNode & ">" & return after _xml 
end writeNode 

getProp xml 
    return _xml 
end xml 

把該腳本在卡上或堆棧然後執行:

startXML 
startTag "employeetable" 
startTag "employee" 
addAttribute "IDNum", 1 
startTag "firstName" 
writeContent "Steve" 
closeTag 
writeNode "lastName", "Jobs" 
writeNode "roomnum", "001" 
writeNode "phoneExt", "345" 
writeNode "parkingSlot", "100" 
closeTag 
closeTag 
put the xml of this card into field 1 

這當然不是一個完整的解決方案,因爲它不會做任何驗證你的輸入和格式可能不是你想要的,但我想它可以讓你開始。

+0

是的,這是我的目標。謝謝。我會繼續這個實現,並會做一些測試。然後我會發布結果。 – 2013-05-08 14:39:26

0

Mark Wieder(公共領域)的版本控制庫包含一些可能考慮的XML編寫過程。

http://revonline2.runrev.com/stack/686/libVersionControl

--> xml 

/** ------------------------------ 
* XML.StartTag 
* 
* Return <tag> 
* Convert embedded commas and spaces as part of the process. 
* ------------------------------ 
*/ 
private function XML.StartTag pTag 
    if comma is in pTag then 
     replace comma with kMagicComma in pTag 
    end if 
    if "&" is in pTag then 
     replace "&" with kAmpersand in pTag 
    end if 
    if space is in pTag and "=" is not in pTag then 
     replace space with kMagicSpace in pTag 
    end if 
    return "<" & pTag & ">" 
end XML.StartTag 

/** ------------------------------ 
* XML.EndTag 
* 
* Return </tag> 
* Convert embedded commas and spaces as part of the process. 
* 
* @pTag : the tag of interest 
* ------------------------------ 
*/ 
private function XML.EndTag pTag 
    if comma is in pTag then 
     replace comma with kMagicComma in pTag 
    end if 
    if space is in pTag and "=" is not in pTag then 
     replace space with kMagicSpace in pTag 
    end if 
    return "</" & pTag & ">" & cr 
end XML.EndTag 

/** ------------------------------ 
* XML.Element 
* 
* return <tag>value</tab> 
* base64encode the value item if necessary 
* ------------------------------ 
*/ 
private function XML.Element pTag, pValue 
    local tIsBinary 
    local tTest 

    put false into tIsBinary 
    if pTag is in "htmlText, script, imagedata" then 
     put true into tIsBinary 
    else 
     repeat for each byte tByte in pValue 
      put chartonum(tByte) into tTest 
      -- see if the char is printable 
      if tTest < 32 or tTest > 128 then 
       put true into tIsBinary 
       -- no need to look further 
       exit repeat 
      end if 
     end repeat 
    end if 
    -- can't have binary data in xml 
    if tIsBinary then 
     put "base64decode(" && q(base64encode(pValue)) && ")" into pValue 
    end if 
    return XML.StartTag(pTag) & pValue & XML.EndTag(pTag) 
end XML.Element 

--> Utilities 

function q pText 
    return quote & pText & quote 
end q 
+1

我應該補充說,雖然我發現使用這樣的函數來生成xml文件比使用內置函數更容易,但內置例程對於讀取和解析xml文件非常方便。 – mwieder 2013-05-06 18:10:38

+0

是的,我正在尋找一個LiveCode ** XML Writing **函數。 – 2013-05-07 09:57:20

0

的LiveCode文件(字典)包含許多XML相關的功能。這些函數足以導航和操作任何XML樹。但是,XML外部並沒有很好地處理索引,這意味着您需要自己跟蹤這些索引。例如。您可以使用它們的索引號遍歷XML樹,檢查每個樹節點的ID號並使用具有正確ID號的節點。讓我知道你是否需要一個例子。

對於其他任何事情,弄清楚庫的工作方式與自己計算XML函數一樣複雜。只需閱讀內置的文檔。

+0

我舉了一個例子,說明這樣一個XML編寫API在問題中的樣子。我正在尋求構建XML文件的簡單方法。看到我的其他答案。 Java的一個例子是http://java.ociweb.com/mark/programming/wax.html。 構建一個DOM結構來描述一個大的XML文檔將不起作用,因爲它不適合內存。即使這樣做,它也不是一個簡單的API使用。 2013-05-07 09:33:57

0

它接縫它不會使用UTF8文本(重音字符,如ąęćś)作爲節點值。

+0

你認爲UTF8不起作用? revNNN功能? – 2013-05-07 14:50:14

+0

我想到了writeContent和addAttribute函數。 – 2013-05-19 11:12:55

+0

只需使用uniDecode(uniEncode(myXMLData,「UTF8」))轉換文件即可。 – Mark 2013-06-15 11:21:10