我应该使用哪种格式的列表来同步文件?
我正在制作一个将资源与服务器同步的移动应用程序。它将具有以下行为:
- 有一个服务器上所有文件的列表
- 有一个远程文件将在本地存储的值
- 有一个校验和可与本地列表进行比较以查看远程文件是否已更改
- 最好,文件应该被加密。 (但是我们当然可以使用https)
我首先想到了最简单的解决方案,使用CSV
来存储值。然后我们考虑了程序的可扩展性,得出的结论是XML
会更加开放。我做了一些谷歌搜索,但没有找到适合我们目的的模式。 RSS
、ATOM
达不到我们的一些要求,并且 XDI
看起来太复杂。
我们应该创建自己的 XML 模式吗?或者是否有一些简单的默认格式适合我们的情况。
谢谢!
回答
我为此制作了一个自定义的 xml 架构,供其他人参考。
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!--Simple Types-->
<xs:simpleType name="remoteurltype">
<xs:restriction base="xs:anyURI">
<xs:pattern value="http(s)?://.*"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="localurltype">
<xs:restriction base="xs:anyURI">
<xs:pattern value="/.*"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="dectype">
<xs:restriction base="xs:decimal">
<xs:pattern value="[0-9]+\.?[0-9]*"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="hashtype">
<xs:restriction base="xs:string">
<xs:pattern value="[a-z0-9]{32}"/>
</xs:restriction>
</xs:simpleType>
<!-- Complex Types -->
<xs:complexType name="resourcetype">
<xs:attribute name="src" type="remoteurltype" use="required"/>
<xs:attribute name="local" type="localurltype" use="required"/>
<xs:attribute name="hash" type="hashtype" use="required"/>
</xs:complexType>
<xs:complexType name="resourcelisttype">
<xs:sequence>
<xs:element name="db" type="resourcetype"/>
<xs:element name="img" maxOccurs="unbounded" type="resourcetype"/>
</xs:sequence>
<xs:attribute name="version" type="dectype" use="required"/>
</xs:complexType>
<!-- Root Element -->
<xs:element name="resources" type="resourcelisttype"/>
</xs:schema>
它已经得到很好的验证,这里有一个例子。
<?xml version="1.0" encoding="UTF-8"?>
<resources version="0.5"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="resources.xsd">
<db src="http://prdownloads.sourceforge.net/souptonuts/sqlite_examples.tar.gz?download"
local="/database/default.sqlite" hash="64846a8f75d56fd68b01f55495ac5986" />
<img src="http://www.google.com/images/logos/ps_logo2.png"
local="/images/google.png" hash="4b9606a40bd81e8a047d2f74fa167e35" />
<img src="http://www.baidu.com/img/baidu_sylogo1.gif"
local="/images/baidu_sylogo1.gif" hash="52137eafacaf179057c837dfa720ecf9" />
</resources>
I am making a mobile app that syncs resources with a server. It will have the following behavior:
- There is a list of all of the files on the server
- There is a value where the remote files will be stored locally
- There is a checksum to compare with the local list to see whether the remote file has changed
- Preferably, the file should be encrypted. (But of course we can use https)
I first thought of the easiest solution, using a CSV
to store values. Then we considered the extensibility of the program, and concluded that XML
would be more open. I did some googling and did not find a schema that fits our purpose. RSS
, ATOM
falls short on some of our requirements, and XDI
looks too complicated.
Should we make our own XML schema? Or is there some simple and default format that will fit our case.
Thanks!
Answer
I made a custom xml schema for this, here it is for anyone else to reference.
<?xml version="1.0" encoding="ISO-8859-1" ?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
<!--Simple Types-->
<xs:simpleType name="remoteurltype">
<xs:restriction base="xs:anyURI">
<xs:pattern value="http(s)?://.*"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="localurltype">
<xs:restriction base="xs:anyURI">
<xs:pattern value="/.*"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="dectype">
<xs:restriction base="xs:decimal">
<xs:pattern value="[0-9]+\.?[0-9]*"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="hashtype">
<xs:restriction base="xs:string">
<xs:pattern value="[a-z0-9]{32}"/>
</xs:restriction>
</xs:simpleType>
<!-- Complex Types -->
<xs:complexType name="resourcetype">
<xs:attribute name="src" type="remoteurltype" use="required"/>
<xs:attribute name="local" type="localurltype" use="required"/>
<xs:attribute name="hash" type="hashtype" use="required"/>
</xs:complexType>
<xs:complexType name="resourcelisttype">
<xs:sequence>
<xs:element name="db" type="resourcetype"/>
<xs:element name="img" maxOccurs="unbounded" type="resourcetype"/>
</xs:sequence>
<xs:attribute name="version" type="dectype" use="required"/>
</xs:complexType>
<!-- Root Element -->
<xs:element name="resources" type="resourcelisttype"/>
</xs:schema>
It is well validated and here is an example.
<?xml version="1.0" encoding="UTF-8"?>
<resources version="0.5"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="resources.xsd">
<db src="http://prdownloads.sourceforge.net/souptonuts/sqlite_examples.tar.gz?download"
local="/database/default.sqlite" hash="64846a8f75d56fd68b01f55495ac5986" />
<img src="http://www.google.com/images/logos/ps_logo2.png"
local="/images/google.png" hash="4b9606a40bd81e8a047d2f74fa167e35" />
<img src="http://www.baidu.com/img/baidu_sylogo1.gif"
local="/images/baidu_sylogo1.gif" hash="52137eafacaf179057c837dfa720ecf9" />
</resources>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
为此,您应该使用自己的 XML 模式。设计和实现新模式的简便性可能是使用 XML 的主要优点。
远离 CSV,因为它会成为字符集、嵌入引号、嵌入新行等持续问题的根源。
You should use your own XML schema for this. The ease with which new schema can be designed and implemented is probably the main advantage of using XML.
Stay away from CSV as it will be a source of constant problems with character sets, embedded quotes, embedded new lines etc.