The Archives

  • 01.Sep.11
    Adding formatting to an XML document shell | rafacas | (0)
    Sometimes, when I have to program a web service client I have to deal with unformatted XML files. For example, the next one: <users><user><email>pfortuny@commandliners.com</email><passwd>a0f901492d89fe2ba88cc96bf9d 2475e</passwd></user><user><email>n0str0m0@commandliners.com</email><passwd>7e1b6dbfa824d 5d114e96981cededd00</passwd></user><user><email>rafacas@commandliners.com</email><passwd> 70c1db56f301c9e337b0099bd4174b28</passwd></user></users> This is not a bad thing, because it is sent that way to save traffic, but I'd rather see it in a human readable format. So I use the xmllint command, that reformat and reindent the input. $ xmllint --format test.xml <?xml version="1.0"?> <users>   <user>     <email>pfortuny@commandliners.com</email>     <passwd>a0f901492d89fe2ba88cc96bf9d2475e</passwd>   </user>   <user>     <email>n0str0m0@commandliners.com</email>     <passwd>7e1b6dbfa824d5d114e96981cededd00</passwd>   </user>   <user>     <email>rafacas@commandliners.com</email>     <passwd>70c1db56f301c9e337b0099bd4174b28</passwd>   </user> </users> The indentation can be controlled by the environment variable XMLLINT_INDENT. The default value is two spaces. pfortuny, n0str0m0, do not worry guys, those are not your ...
  • 18.Aug.11
    Determining if an XML document is well-formed shell | rafacas | (0)
    After creating an XML document from scratch I always check if it is well-formed. This means it must adhere to a number of rules, including the following: Every start-tag must have a matching end-tag. Elements may nest, but may not overlap. There must be exactly one root element. Attribute values must be quoted. An element may not have two attributes with the same name. This is not an exhaustive list and I do not mean to explain all the rules. There are many, many ways a document can be malformed. But if you need to determine if and XML document is well-formed there is a linux ...