Skip to content

ParsXmlHuge flag to support large XML files #24

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions examples/huge_text_node.xml

Large diffs are not rendered by default.

14 changes: 13 additions & 1 deletion libxml2.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ package xsdvalidate
#define GO_ERR_INIT 1024
#define P_ERR_DEFAULT 1
#define P_ERR_VERBOSE 2
#define P_XML_HUGE 4
#define LIBXML_STATIC
#define NOOP ((void)0)

Expand Down Expand Up @@ -249,6 +250,16 @@ static struct xsdParserResult cParseMemSchema(const void* xsd,
return parseSchema(schemaParserCtxt, options);
}

static int getLibxmlOptions(short int options) {
int xmlParserOptions = 0;

if (options & P_XML_HUGE) {
xmlParserOptions |= XML_PARSE_HUGE;
}

return xmlParserOptions;
}

static struct xmlParserResult cParseDoc(const void* goXmlSource,
const int goXmlSourceLen,
const short int options) {
Expand Down Expand Up @@ -282,7 +293,8 @@ static struct xmlParserResult cParseDoc(const void* goXmlSource,
xmlSetGenericErrorFunc(NULL, noOutputCallback);
}

doc = xmlReadMemory(goXmlSource, goXmlSourceLen, NULL, NULL, 0);
int xmlParserOptions = getLibxmlOptions(options);
doc = xmlReadMemory(goXmlSource, goXmlSourceLen, NULL, NULL, xmlParserOptions);

xmlFreeParserCtxt(xmlParserCtxt);
if (doc == NULL) {
Expand Down
3 changes: 2 additions & 1 deletion validate_xsd.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// Package xsdvalidate is a go package for xsd validation that utilizes libxml2.

//The goal of this package is to preload xsd files into memory and to validate xml (fast) using libxml2, like post bodys of xml service endpoints or api routers. At the time of writing, similar packages I found on github either didn't provide error details or got stuck under load. In addition to providing error strings it also exposes some fields of libxml2 return structs.
// The goal of this package is to preload xsd files into memory and to validate xml (fast) using libxml2, like post bodys of xml service endpoints or api routers. At the time of writing, similar packages I found on github either didn't provide error details or got stuck under load. In addition to providing error strings it also exposes some fields of libxml2 return structs.
package xsdvalidate

import "C"
Expand Down Expand Up @@ -40,6 +40,7 @@ type Options uint8
const (
ParsErrDefault Options = 1 << iota // Default parser error output
ParsErrVerbose // Verbose parser error output, considerably slower!
ParsXmlHuge // Enable parsing of large XML
)

// Validation options for possible future enhancements.
Expand Down
21 changes: 21 additions & 0 deletions validate_xsd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,27 @@ func TestXmlMemHandlerFail(t *testing.T) {
defer handler.Free()
}

func TestXmlMemHandlerLargeXml(t *testing.T) {
Init()
defer Cleanup()

xmlFilePass, err := os.Open("examples/huge_text_node.xml")
if err != nil {
fmt.Printf("Error: %s %s\n", t.Name(), err.Error())
return
}
defer xmlFilePass.Close()

inXml, _ := ioutil.ReadAll(xmlFilePass)

handler, err := NewXmlHandlerMem(inXml, ParsXmlHuge)
if err != nil {
fmt.Printf("Error: %s %s\n", t.Name(), err.Error())
t.Fail()
}
defer handler.Free()
}

func TestValidateWithXsdHandlerPass(t *testing.T) {
Init()
defer Cleanup()
Expand Down