如何从xsd文件创建一个字符串树

时间:2013-08-07 21:24:24

标签: java xsd xerces

我正在尝试解析xsd文件并使用它的层次结构创建一个字符串树。 我读了以下问题: Parsing XSD and getting nested elements Java API to parse XSD schema file。 但是,我仍然没有得到它。 我写了以下代码:

public class Tree {
    public String Data;
    private Tree Parent;
    private ArrayList<Tree> Children;

    public Tree(String data) {
        Data = data;
        Parent = null;
        Children = new ArrayList<Tree>();
    }

    public Tree AddChild(String data){
        Tree child = new Tree(data);
        child.Parent = this;
        Children.add(child);
        return child;
    }

    public ArrayList<Tree> Children(){
        return Children;
    }

    public Tree Parent(){
        return Parent;
    }

    public static Tree MakeTreeFromXsd(String xsdPath) 
            throws ClassNotFoundException, InstantiationException, 
            IllegalAccessException, ClassCastException{

        System.setProperty(DOMImplementationRegistry.PROPERTY, 
                "com.sun.org.apache.xerces.internal.dom.DOMXSImplementationSourceImpl");

        DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();

        com.sun.org.apache.xerces.internal.xs.XSImplementation impl = 
                (com.sun.org.apache.xerces.internal.xs.XSImplementation) 
                registry.getDOMImplementation("XS-Loader");

        XSLoader schemaLoader = impl.createXSLoader(null);
        XSModel model = schemaLoader.loadURI(xsdPath);
        XSNamedMap map = model.getComponents(XSConstants.ELEMENT_DECLARATION);
        Tree t = new Tree("root");
        for (int j=0; j<map.getLength(); j++) {
            XSObject o = map.item(j);
            t.AddChild(o.getName());
        }
        return t;
    }
}

如何改进MakeTreeFromXsd函数以返回具有xsd所有层次结构的树,而不仅仅是顶层组件?

0 个答案:

没有答案