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

No error or panic when parsing non-XML #86

Open
tuan-nxcr opened this issue Aug 15, 2022 · 4 comments
Open

No error or panic when parsing non-XML #86

tuan-nxcr opened this issue Aug 15, 2022 · 4 comments

Comments

@tuan-nxcr
Copy link

tuan-nxcr commented Aug 15, 2022

I was expecting Parse or Query to error out if I were to pass in something completely invalid, but it falls all the way through instead:

func TestXmlParse_1(t *testing.T) {
	//s := `<note>
	//<to>John</to>
	//<from>Smith</from>
	//<heading>Reminder</heading>
	//<body>Don't forget me this weekend!</body>
	//</note>`

	s := `{"NotXml":"ActuallyJson"}`

	parse, err := xmlquery.Parse(strings.NewReader(s))
	if err != nil {
		println(err.Error())
		t.Fail()
	}
	query, err := xmlquery.Query(parse, "//body/text()")
	if err != nil {
		println(err.Error())
		t.Fail()
	}
	println(query.Data)

}

using:

  • github.com/antchfx/xmlquery v1.3.11
@zhengchun
Copy link
Contributor

Add if query != nil {} to avoid throw a query is nil exception before println(query.Data)

@tuan-nxcr
Copy link
Author

tuan-nxcr commented Aug 16, 2022

@zhengchun this is more of a question why Parse can proceed without any error when I give it invalid XML, not about how to handle a nil pointer exception.

@zhengchun
Copy link
Contributor

Sorry. In parse processing, we using https://pkg.go.dev/encoding/xml#NewDecoder to parsing, no extract additional method to check the input document whether is XML or JSON.

@zhengchun zhengchun reopened this Aug 16, 2022
@tuan-nxcr
Copy link
Author

tuan-nxcr commented Aug 16, 2022

@zhengchun

Thanks for reopening! Yes, I was just looking for some way to validate that we have at least well-formed XML before proceeding to parsing (similar to what is recommended by the authors of the gjson package https://github.com/tidwall/gjson#validate-json).

My workaround for this right now is to precede the xmlquery.Parse step with:

func isXml(someString string) bool {
    return xml.Unmarshal([]byte(someString), new(interface{})) == nil
}

(the above was inspired by this Stackoverflow answer, which I will say is quite unintuitive)

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

No branches or pull requests

2 participants