tail-f-systems / JNC

JNC (Java NETCONF Client) is the name of a Java library for communicating with NETCONF agents, and a plugin for pyang (http://code.google.com/p/pyang/) to generate Java classes from YANG models, to be used by the JNC library.
Other
77 stars 87 forks source link

Parsing saved XML #11

Closed hshorter closed 11 years ago

hshorter commented 11 years ago

When saving XML we will need to parse this back into a NodeSet with our own types in. The code below shows casting an Element to an Equipment type after reading from a device. (The use of the 'delimeter' will be explained in another bug). The cast is successful on the NodeSet returned from the NetconfSession, but when I try to save that XML and use it to build a new NodeSet the children are all Elements and cannot be cast into our model types.

    NodeSet all = session.getConfig();

    Equipment equipment = (Equipment) all.get(0);

    StringBuilder savedXML = new StringBuilder();
    Iterator it = all.iterator();

    while (it.hasNext()) {
        Element object = (Element)it.next();
        savedXML.append(object.toXMLString());
        savedXML.append("</delimeter>");
    }
    NodeSet nodeSet = new NodeSet();
    String[] configElements = savedXML.toString().split("</delimeter>");
    for (int i = 0; i < configElements.length; i++) {
        Element element = new XMLParser().parse(configElements[i]);
        nodeSet.add(element);
    }

    Equipment equip = (Equipment)nodeSet.get(0); //This results in a ClassCastException
hshorter commented 11 years ago

Looks like I was using the wrong parser. When using the YangXMLParser the correct class types are used. Not sure why there are two XMLParsers though?

danieldai commented 6 years ago

@hshorter thanks, I met the same problem, YangXMLParser is the right answer