Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Failed to parse chardata. #129

Open
yuanhuili opened this issue Jan 22, 2021 · 2 comments
Open

Failed to parse chardata. #129

yuanhuili opened this issue Jan 22, 2021 · 2 comments

Comments

@yuanhuili
Copy link

  • I have an xml file, and a part of the string is as follows:
    <userdatatype>module
        <dataTypeEnum schemanode="" description="" declareType="ExportAll" name="ISOLATIONMODE"/>
    </userdatatype>

When I parse the xml above using the following code ,the code is as follows:

	ele,err:=xmltree.Parse(data)
	xmltree.Unmarshal(ele,v)
	if err != nil {
		fmt.Printf("error: %v", err)
		return
	}

The corresponding structure is as follows:

type Cuserdatatype struct {
	XMLName       xml.Name       `sxml:"userdatatype,omitempty" json:"userdatatype,omitempty"`
	CdataTypeEnum *CdataTypeEnum `xml:"dataTypeEnum,omitempty" json:"dataTypeEnum,omitempty"`
	CString        string         `xml:",chardata" json:",omitempty"`
}

The parsed result "module" is lost!!
image

ps:but when I use the native parser. "moudle" is not lost.

 xml.Unmarshal(data, v)

image

Why?

@yuanhuili
Copy link
Author

ps:v := &Cmodule{}

@droyo
Copy link
Owner

droyo commented Jan 22, 2021

It's a bug. This piece of code in marshal.go

if len(el.Children) == 0 {
	if len(el.Content) > 0 {
		e.w.Write(el.Content)
	} else {
		return nil
	}
}

The Marshal function only writes out .Content (which includes the chardata) if there are no child elements. I could write out .Content unconditionally, but the problem is that .Content also contains the XML for child elements, and it will not reflect any changes made to the tree after Parse.

I'm not sure what's best here. I guess we could include xml.CharData in the decode switch here, add a .CharData field to Element, and work that into the encoder. The Marshal output would look different from the source document, but I think it should be good enough for the example here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants