Exemplo n.º 1
0
func TestParseErr(t *testing.T) {
	_, err := xmltree.ParseXML(bytes.NewBufferString("<p1/>"))
	if err.Error() != "Malformed XML file" {
		t.Error("Incorrect error message:", err.Error())
	}

	_, err = xmltree.ParseXML(bytes.NewBufferString(""))
	if err.Error() != "EOF" {
		t.Error("Incorrect error message:", err.Error())
	}

	_, err = xmltree.ParseXML(bytes.NewBufferString("<p1/>"), func(s *xmltree.ParseOptions) {
		s.Strict = false
	})
	if err != nil {
		t.Error("Error not nil:", err.Error())
	}
}
Exemplo n.º 2
0
func NewXMLRecordsProcessor(recordsReader io.Reader, cdrPath utils.HierarchyPath, timezone string, httpSkipTlsCheck bool, cdrcCfgs []*config.CdrcConfig) (*XMLRecordsProcessor, error) {
	xp, err := goxpath.Parse(cdrPath.AsString("/", true))
	if err != nil {
		return nil, err
	}
	optsNotStrict := func(s *xmltree.ParseOptions) {
		s.Strict = false
	}
	xmlNode, err := xmltree.ParseXML(recordsReader, optsNotStrict)
	if err != nil {
		return nil, err
	}
	xmlProc := &XMLRecordsProcessor{cdrPath: cdrPath, timezone: timezone, httpSkipTlsCheck: httpSkipTlsCheck, cdrcCfgs: cdrcCfgs}
	xmlProc.cdrXmlElmts = goxpath.MustExec(xp, xmlNode, nil)
	return xmlProc, nil
}
Exemplo n.º 3
0
// getElementText will process the node to extract the elementName's text out of it (only first one found)
// returns utils.ErrNotFound if the element is not found in the node
func elementText(xmlRes tree.Res, elmntPath string) (string, error) {
	xp, err := goxpath.Parse(elmntPath)
	if err != nil {
		return "", err
	}
	elmntBuf := bytes.NewBufferString(xml.Header)
	if err := goxpath.Marshal(xmlRes.(tree.Node), elmntBuf); err != nil {
		return "", err
	}
	elmntNode, err := xmltree.ParseXML(elmntBuf)
	if err != nil {
		return "", err
	}
	elmnts, err := goxpath.Exec(xp, elmntNode, nil)
	if err != nil {
		return "", err
	}
	if len(elmnts) == 0 {
		return "", utils.ErrNotFound
	}
	return elmnts[0].String(), nil
}