XML attribute vs XML element

前端 未结 20 2596
暖寄归人
暖寄归人 2020-11-22 01:34

At work we are being asked to create XML files to pass data to another offline application that will then create a second XML file to pass back in order to update some of ou

20条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-22 02:17

    It may depend on your usage. XML that is used to represent stuctured data generated from a database may work well with ultimately field values being placed as attributes.

    However XML used as a message transport would often be better using more elements.

    For example lets say we had this XML as proposed in the answer:-

    
       
          XYX
          
             YYZ
          
        
    
    

    Now we want to send the ITEM element to a device to print he barcode however there is a choice of encoding types. How do we represent the encoding type required? Suddenly we realise, somewhat belatedly, that the barcode wasn't a single automic value but rather it may be qualified with the encoding required when printed.

       
          something
          XYX
          
             YYZ
          
       
    

    The point is unless you building some kind of XSD or DTD along with a namespace to fix the structure in stone, you may be best served leaving your options open.

    IMO XML is at its most useful when it can be flexed without breaking existing code using it.

提交回复
热议问题