Beispiel #1
0
func newDoc(buf []byte, idattrs []XMLIDOption) (*C.xmlDoc, error) {
	ctx := C.xmlCreateMemoryParserCtxt((*C.char)(unsafe.Pointer(&buf[0])),
		C.int(len(buf)))
	if ctx == nil {
		return nil, mustPopError()
	}
	defer C.xmlFreeParserCtxt(ctx)

	C.xmlParseDocument(ctx)

	if ctx.wellFormed == C.int(0) {
		return nil, mustPopError()
	}

	doc := ctx.myDoc
	if doc == nil {
		return nil, mustPopError()
	}

	for _, idattr := range idattrs {
		addIDAttr(C.xmlDocGetRootElement(doc),
			idattr.AttributeName, idattr.ElementName, idattr.ElementNamespace)
	}
	return doc, nil
}
Beispiel #2
0
func (p *Parser) ParseString(s string) (*Document, error) {
	ctx := C.xmlCreateMemoryParserCtxt(C.CString(s), C.int(len(s)))
	if ctx == nil {
		return nil, errors.New("error createing parser")
	}
	defer C.xmlFreeParserCtxt(ctx)

	C.xmlCtxtUseOptions(ctx, C.int(p.Options))
	C.xmlParseDocument(ctx)

	if ctx.wellFormed == C.int(0) {
		return nil, errors.New("malformed XML")
	}

	doc := ctx.myDoc
	if doc == nil {
		return nil, errors.New("parse failed")
	}
	return wrapDocument(doc), nil
}
Beispiel #3
0
// xmlFreeParserCtxt
func (p *Parser) Free() {
	C.xmlFreeParserCtxt(p.Ptr)
}