I got something working.

It's not perfect.. but for a sample that's fine.


This code will list the elements that are not in the schema


package ca.sebastiendionne.upnp.device;

import java.io.File;

import org.apache.xmlbeans.QNameSet;
import org.apache.xmlbeans.SchemaProperty;
import org.apache.xmlbeans.SchemaType;
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.impl.values.XmlAnyTypeImpl;
import org.upnp.schemas.device_1_0.RootDocument;
import org.w3c.dom.Attr;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class ExtraTagsDeviceTest {

    public static void main(String[] args) throws Exception {

        ExtraTagsDeviceTest mod = new ExtraTagsDeviceTest();

        mod.go();

    }

    public void browse(XmlObject xo, NodeList childList) {

        for (int i = 0; i < childList.getLength(); i++) {
            Node node = childList.item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                boolean isInXsd = isAttributeInSchema(xo,
node.getLocalName());

                if (!isInXsd) {
                    System.out.println("element name=" + node.getLocalName()
+ "  IS IN SCHEMA=" + isInXsd);

                    System.out.println("Node Name: " + node.getNodeName());
                    System.out.println("Node NS: " + node.getNamespaceURI()
+ "-" + node.getNodeValue());
                    System.out.println("Attributes: ");
                    NamedNodeMap nnm = node.getAttributes();
                    if (nnm != null) {
                        int len = nnm.getLength();
                        Attr attr;
                        for (int j = 0; j < len; j++) {
                            attr = (Attr) nnm.item(j);
                            System.out.println("\t " + attr.getNodeName() +
"=\"" + attr.getNodeValue() + '"');
                        }
                    }
                    System.out.println("End of Attributes");

                    NodeList list = node.getChildNodes();

                    if (list.getLength() == 1) {
                        System.out.println("Value=" +
list.item(0).getNodeValue());
                    }

                }

            }
        }
    }

    public boolean editExistingDocWithDOM(XmlObject xo) {

        NodeList childList = xo.getDomNode().getChildNodes();

        browse(xo, childList);

        return true;
    }

    private void go() throws Exception {

        RootDocument atd = RootDocument.Factory.parse(new
File("./src/test/resources/device2.xml"));

        editExistingDocWithDOM(atd.getRoot().getDevice());

    }

    private static boolean isAttributeInSchema(XmlObject xo, String name) {

        boolean ret = false;

        SchemaType xoSt = xo.schemaType();

        SchemaProperty[] xoSps = xoSt.getElementProperties();

        if (xoSps.length > 0) {
            for (int i = 0; i < xoSps.length; i++) {
                SchemaProperty xoSp = xoSps[i];

                if (xoSp.getName().getLocalPart().equals(name)) {
                    ret = true;
                    break;
                }
            }
        }

        return ret;

    }

}


-------------
A+

Sébastien.

Vous pouvez me suivre sur Twitter / You can follow me on Twitter :
http://twitter.com/survivant

Reply via email to