Exemplo n.º 1
0
// xmlReadFile
func ReadFile(filename string, encoding string, options ParserOption) *Document {
	ptrf := C.CString(filename)
	defer C.free_string(ptrf)
	ptre := C.CString(encoding)
	defer C.free_string(ptre)
	doc := C.xmlReadFile(ptrf, ptre, C.int(options))
	return makeDoc(doc)
}
Exemplo n.º 2
0
// ReadFile loads an XmlDocument from a filename. The encoding declared in the document will be
// used as the input encoding. If no encoding is declared, the library will use the alogrithm
// in the XML standard to determine if the document is encoded with UTF-8 or UTF-16.
func ReadFile(filename string, options ParseOption) (doc *XmlDocument, err error) {
	// verify the file exists and can be read before we invoke C API
	_, err = os.Stat(filename)
	if err != nil {
		return
	}

	dataBytes := GetCString([]byte(filename))
	dataPtr := unsafe.Pointer(&dataBytes[0])
	var docPtr *C.xmlDoc
	docPtr = C.xmlReadFile((*C.char)(dataPtr), nil, C.int(options))
	if docPtr == nil {
		err = ERR_FAILED_TO_PARSE_XML
	} else {
		var encoding []byte
		// capture the detected input encoding
		p := docPtr.encoding
		if p != nil {
			encoding = []byte(C.GoString((*C.char)(unsafe.Pointer(p))))
		}
		doc = NewDocument(unsafe.Pointer(docPtr), 0, encoding, DefaultEncodingBytes)
	}
	return
}