1 package org.opendaylight.controller.sal.connect.netconf;
4 import java.util.ArrayList;
7 import org.opendaylight.yangtools.yang.common.QName;
8 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
9 import org.opendaylight.yangtools.yang.data.api.Node;
10 import org.opendaylight.yangtools.yang.data.impl.CompositeNodeTOImpl;
11 import org.opendaylight.yangtools.yang.data.impl.SimpleNodeTOImpl;
12 import org.w3c.dom.Document;
13 import org.w3c.dom.Element;
14 import org.w3c.dom.NodeList;
16 public class XmlDocumentUtils {
18 public static CompositeNode toCompositeNode(Document doc) {
19 return (CompositeNode) toCompositeNode(doc.getDocumentElement());
22 private static Node<?> toCompositeNode(Element element) {
23 String orgNamespace = element.getNamespaceURI();
24 URI biNamespace = null;
25 if (orgNamespace != null) {
26 biNamespace = URI.create(orgNamespace);
28 QName qname = new QName(biNamespace, element.getLocalName());
30 List<Node<?>> values = new ArrayList<>();
31 NodeList nodes = element.getChildNodes();
32 boolean isSimpleObject = false;
34 for (int i = 0; i < nodes.getLength(); i++) {
35 org.w3c.dom.Node child = nodes.item(i);
36 if (child instanceof Element) {
37 isSimpleObject = false;
38 values.add(toCompositeNode((Element) child));
40 if (!isSimpleObject && child instanceof org.w3c.dom.Text) {
41 value = element.getTextContent();
42 if (value.matches(".*\\w.*")) {
43 isSimpleObject = true;
50 return new SimpleNodeTOImpl<>(qname, null, value);
52 return new CompositeNodeTOImpl(qname, null, values);