2013-05-06 117 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