1

xsd ファイルを解析し、その階層を持つ文字列のツリーを作成しようとしています。次の質問を読みました: XSD の解析とネストされた要素の取得XSD スキーマ ファイルを解析するための Java API。しかし、私はまだそれを理解していません。次のコードを書きました。

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 のすべての階層を含むツリーを返すにはどうすればよいですか?

4

0 に答える 0