How to serialize a graph structure?

前端 未结 6 913
一整个雨季
一整个雨季 2021-02-02 08:11

Flat files and relational databases give us a mechanism to serialize structured data. XML is superb for serializing un-structured tree-like data.

But many problems are b

6条回答
  •  庸人自扰
    2021-02-02 09:11

    Typically relationships in XML are shown by the parent/child relationship. XML can handle graph data but not in this manner. To handle graphs in XML you should use the xs:ID and xs:IDREF schema types.

    In an example, assume that node/@id is an xs:ID type and that link/@ref is an xs:IDREF type. The following XML shows the cycle of three nodes 1 -> 2 -> 3 -> 1.

    
       
        
      
      
        
      
      
        
      
    
    

    Many development tools have support for ID and IDREF too. I have used Java's JAXB (Java XML Binding. It supports these through the @XmlID and the @XmlIDREF annotations. You can build your graph using plain Java objects and then use JAXB to handle the actual serialization to XML.

自定义标题
段落格式
字体
字号
代码语言
提交回复
热议问题