コード例 #1
0
// Cobbler's errors come in an XML document which contains
// both an error code and an error message. This snippet converts
// that document into a proper Go error object.
// For more detail on how that document looks like check
// `./fixtures/login-res-err.xml`
func errorInCobbler(body []byte) error {
	path := xmlpath.MustCompile("//member/value")
	rootNode, err := xmlpath.Parse(bytes.NewBuffer(body))
	if err != nil {
		return err
	}

	found := 0
	numberOfNodesToFind := 2
	elements := make([]string, numberOfNodesToFind)
	iterator := path.Iter(rootNode)

	for iterator.Next() {
		if found < numberOfNodesToFind {
			elements[found] = iterator.Node().String()
			found++
		}
	}

	if found == 2 {
		return fmt.Errorf("error %s: %s", elements[0], elements[1])
	}

	return nil
}
コード例 #2
0
// Find the given xpath in the given document.
// params:
// - xpath: the xpath to look for
// - doc:   the XML document to inspect. Must be an array of bytes.
func findXPath(xpath string, doc []byte) (string, error) {
	path := xmlpath.MustCompile(xpath)
	rootNode, err := xmlpath.Parse(bytes.NewBuffer(doc))
	if err != nil {
		return "", err
	}

	if value, ok := path.String(rootNode); ok {
		return value, nil
	}

	return "", fmt.Errorf("node blank or not found\n%s", rootNode.String())
}