예제 #1
0
파일: xmlsec.go 프로젝트: crewjam/go-xmlsec
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
}
예제 #2
0
파일: xml.go 프로젝트: grmartin/go-libxml2
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
}