github.com/gogf/gf@v1.16.9/encoding/gxml/gxml.go (about)

     1  // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
     2  //
     3  // This Source Code Form is subject to the terms of the MIT License.
     4  // If a copy of the MIT was not distributed with this file,
     5  // You can obtain one at https://github.com/gogf/gf.
     6  
     7  // Package gxml provides accessing and converting for XML content.
     8  package gxml
     9  
    10  import (
    11  	"strings"
    12  
    13  	"github.com/clbanning/mxj"
    14  	"github.com/gogf/gf/encoding/gcharset"
    15  	"github.com/gogf/gf/text/gregex"
    16  )
    17  
    18  // Decode parses <content> into and returns as map.
    19  func Decode(content []byte) (map[string]interface{}, error) {
    20  	res, err := convert(content)
    21  	if err != nil {
    22  		return nil, err
    23  	}
    24  	return mxj.NewMapXml(res)
    25  }
    26  
    27  // DecodeWithoutRoot parses <content> into a map, and returns the map without root level.
    28  func DecodeWithoutRoot(content []byte) (map[string]interface{}, error) {
    29  	res, err := convert(content)
    30  	if err != nil {
    31  		return nil, err
    32  	}
    33  	m, err := mxj.NewMapXml(res)
    34  	if err != nil {
    35  		return nil, err
    36  	}
    37  	for _, v := range m {
    38  		if r, ok := v.(map[string]interface{}); ok {
    39  			return r, nil
    40  		}
    41  	}
    42  	return m, nil
    43  }
    44  
    45  // Encode encodes map <m> to a XML format content as bytes.
    46  // The optional parameter <rootTag> is used to specify the XML root tag.
    47  func Encode(m map[string]interface{}, rootTag ...string) ([]byte, error) {
    48  	return mxj.Map(m).Xml(rootTag...)
    49  }
    50  
    51  // Encode encodes map <m> to a XML format content as bytes with indent.
    52  // The optional parameter <rootTag> is used to specify the XML root tag.
    53  func EncodeWithIndent(m map[string]interface{}, rootTag ...string) ([]byte, error) {
    54  	return mxj.Map(m).XmlIndent("", "\t", rootTag...)
    55  }
    56  
    57  // ToJson converts <content> as XML format into JSON format bytes.
    58  func ToJson(content []byte) ([]byte, error) {
    59  	res, err := convert(content)
    60  	if err != nil {
    61  		return nil, err
    62  	}
    63  	mv, err := mxj.NewMapXml(res)
    64  	if err == nil {
    65  		return mv.Json()
    66  	} else {
    67  		return nil, err
    68  	}
    69  }
    70  
    71  // convert converts the encoding of given XML content from XML root tag into UTF-8 encoding content.
    72  func convert(xml []byte) (res []byte, err error) {
    73  	patten := `<\?xml.*encoding\s*=\s*['|"](.*?)['|"].*\?>`
    74  	matchStr, err := gregex.MatchString(patten, string(xml))
    75  	if err != nil {
    76  		return nil, err
    77  	}
    78  	xmlEncode := "UTF-8"
    79  	if len(matchStr) == 2 {
    80  		xmlEncode = matchStr[1]
    81  	}
    82  	xmlEncode = strings.ToUpper(xmlEncode)
    83  	res, err = gregex.Replace(patten, []byte(""), xml)
    84  	if err != nil {
    85  		return nil, err
    86  	}
    87  	if xmlEncode != "UTF-8" && xmlEncode != "UTF8" {
    88  		dst, err := gcharset.Convert("UTF-8", xmlEncode, string(res))
    89  		if err != nil {
    90  			return nil, err
    91  		}
    92  		res = []byte(dst)
    93  	}
    94  	return res, nil
    95  }