go parses svn log generated files in xml format

  • 2020-05-27 05:52:08
  • OfStack

Want to use go to do a statistical svn code submission tool, similar to statsvn.

Progress today by using analytical svn go log generated xml format file, in go doc found a Example, 1 is included some typical analysis case, but at the time of slavish or has a problem, there is a paths have multiple path, each path has its own properties, and values, to channel properties and values at the same time, it's with great effort.

First, demo in svn xml format log.

Example


<?xml version="1.0"?>
<log>
<logentry
  revision="43424">
<author>fukun</author>
<date>2015-03-13T11:05:17.341130Z</date>
<paths>
<path
  kind="file"
  action="M">/BigDataPlatform/trunk/application/controllers/Enterpriseajaxapi.php</path>
<path
  kind="file"
  action="M">/BigDataPlatform/trunk/application/models/ChartDataFormater.php</path>
<path
  kind="file"
  action="M">/BigDataPlatform/trunk/application/controllers/Mediaajaxapi.php</path>
</paths>
<msg> Abstract the   A method of formatting line graphs </msg>
</logentry>
<logentry
  revision="43423">
<author>zhengjin</author>
<date>2015-03-13T11:04:50.450051Z</date>
<paths>
<path
  kind="file"
  action="M">/BigDataPlatform/trunk/public/static/js/mapChart.js</path>
</paths>
<msg> Map event response </msg>
</logentry>
</log>

Everything else was fine, but I had trouble parsing the paths section. If... Paths [] Path ` xml: "paths `..." .

Example


type Path struct {
  Kind string `xml:"kind,attr"`
  Action string `xml:"action,attr"`
  Path string `xml:"Path"`
} 

In this case, you can only get 1 value of path, but if you use Paths []Path 'xml: "paths" > path "', you can only get the values of Action and Kind, but not value of path. After looking at a few more examples on stackoverflow, I found a usage for chardata, and realized that it was still possible to use value in this way. Change the definition of xml of struct Path to 'xml: ",chardata "', and you can get value successfully.

I will not write the specific struct. If necessary, you can go to GoStatsvn which I am developing. GitHub.

Reference:
The data structure associated with XML

1) type Name struct {
Space, Local string
}
Local for the local name and Space for the namespace prefix (namespace identifier). This type does not provide any method. The main use is to define a type on the XML root element

Attr (properties), CharData (character data), Comment (comments), ProcInst (processing instructions), etc. These types are all representations of the XML standard definition, and you can check out the XML standard reference.


Related articles: