Home  >  Article  >  Backend Development  >  Unmarshaling XML with some unknown tags in golang?

Unmarshaling XML with some unknown tags in golang?

WBOY
WBOYforward
2024-02-08 22:45:091138browse

在 golang 中使用一些未知标签解组 XML?

In golang, we often need to process XML data. However, sometimes we may encounter some unknown tags, which brings some difficulties to unmarshaling XML data. So, how to deal with these unknown tags in golang? In this article, PHP editor Xinyi will introduce some methods of handling unknown tags in golang to help you better unmarshal XML data. Whether you are a newbie or an experienced developer, this article can provide you with useful tips and guidance. let's start!

Question content

Given an XML raw string of unknown structure (from OCR result), how should I unmarshal the string into a processable go structure/interface?

With JSON I can do something like this, so is there an XML version of the answer?

Example

randomOcrXmlString := `
<container>
<x></x>
<y><z></z><y>
<abc></abc>
... (many more random tags)
</container>`

My actual intention - insert a tag before the closing 2706a1e7c77ff18e07182931ad8578f7 tag

Workaround

According to encoding/xml document,

So the following works for me

type xmlResponse struct {
    Fields []byte `xml:",innerxml"`
}

func isXMLStringValid(str string) bool {
    // ref https://stackoverflow.com/a/62869933
    decoder := xml.NewDecoder(strings.NewReader(str))
    for {
        err := decoder.Decode(new(interface{}))
        if err != nil {
            return err == io.EOF
        }
    }
}

func parseRawXMLString(xmlStr string) (*xmlResponse, error) {
  if !isXMLStringValid(xmlStr) {
        return nil, errors.New(fmt.Sprintf("xml: construct: input is not valid xml: %s", xmlStr))
    }

  var xmlResp = xmlResponse{}
  err := xml.Unmarshal([]byte(xmlStr), &xmlResp)
  if err != nil {
        log.Printf("xml: unmarshal: %s", err)
        return nil, err
  }
  return &xmlResp, nil
}

The above is the detailed content of Unmarshaling XML with some unknown tags in golang?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:stackoverflow.com. If there is any infringement, please contact admin@php.cn delete