refactor func parseXML
This commit is contained in:
		
							parent
							
								
									46bd8fb68a
								
							
						
					
					
						commit
						542e8e73bc
					
				
							
								
								
									
										14
									
								
								requests.go
									
									
									
									
									
								
							
							
						
						
									
										14
									
								
								requests.go
									
									
									
									
									
								
							| @ -1,7 +1,6 @@ | |||||||
| package gowebdav | package gowebdav | ||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
| 	"encoding/xml" |  | ||||||
| 	"errors" | 	"errors" | ||||||
| 	"fmt" | 	"fmt" | ||||||
| 	"io" | 	"io" | ||||||
| @ -60,16 +59,7 @@ func (c *Client) Propfind(path string, self bool, body string, resp interface{}, | |||||||
| 		return errors.New(fmt.Sprintf("%s - %s %s", rs.Status, rq.Method, rq.URL.String())) | 		return errors.New(fmt.Sprintf("%s - %s %s", rs.Status, rq.Method, rq.URL.String())) | ||||||
| 	} | 	} | ||||||
| 
 | 
 | ||||||
| 	decoder := xml.NewDecoder(rs.Body) | 	parseXML(rs.Body, resp, parse) | ||||||
| 	for t, _ := decoder.Token(); t != nil; t, _ = decoder.Token() { | 
 | ||||||
| 		switch se := t.(type) { |  | ||||||
| 		case xml.StartElement: |  | ||||||
| 			if se.Name.Local == "response" { |  | ||||||
| 				if e := decoder.DecodeElement(resp, &se); e == nil { |  | ||||||
| 					parse(resp) |  | ||||||
| 				} |  | ||||||
| 			} |  | ||||||
| 		} |  | ||||||
| 	} |  | ||||||
| 	return nil | 	return nil | ||||||
| } | } | ||||||
|  | |||||||
							
								
								
									
										15
									
								
								utils.go
									
									
									
									
									
								
							
							
						
						
									
										15
									
								
								utils.go
									
									
									
									
									
								
							| @ -2,6 +2,7 @@ package gowebdav | |||||||
| 
 | 
 | ||||||
| import ( | import ( | ||||||
| 	"bytes" | 	"bytes" | ||||||
|  | 	"encoding/xml" | ||||||
| 	"io" | 	"io" | ||||||
| 	"strconv" | 	"strconv" | ||||||
| 	"strings" | 	"strings" | ||||||
| @ -31,3 +32,17 @@ func parseModified(s *string) time.Time { | |||||||
| 	} | 	} | ||||||
| 	return time.Unix(0, 0) | 	return time.Unix(0, 0) | ||||||
| } | } | ||||||
|  | 
 | ||||||
|  | func parseXML(data io.Reader, resp interface{}, parse func(resp interface{})) { | ||||||
|  | 	decoder := xml.NewDecoder(data) | ||||||
|  | 	for t, _ := decoder.Token(); t != nil; t, _ = decoder.Token() { | ||||||
|  | 		switch se := t.(type) { | ||||||
|  | 		case xml.StartElement: | ||||||
|  | 			if se.Name.Local == "response" { | ||||||
|  | 				if e := decoder.DecodeElement(resp, &se); e == nil { | ||||||
|  | 					parse(resp) | ||||||
|  | 				} | ||||||
|  | 			} | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | } | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user