用类信息解析XML并通过反射进行实例化



我有两个XML文件,其中包含一些类的信息。解析XML后,我想通过反射实例化这些类。

我用DOM和递归解析xml。我想知道的是哪一种是实现它的最通用的方法。这是传递信息和构建GUI的最佳方式。

我真的想不出其他的事情,除了许多IF…像这样:

if (node.getNodeName() == "class") { Class cls = Class.forName(node.getNodeValue()); })

语句,但我不认为这是最佳的方式。

dom解析器

 for (int count = 0; count < nodeList.getLength(); count++) {
        Node tempNode = nodeList.item(count);
        // make sure it's element node.
        if (tempNode.getNodeType() == Node.ELEMENT_NODE) {
            // get node name and value
            System.out.println("nNode Name =" + tempNode.getNodeName() + " [OPEN]");
            System.out.println("Node Value =" + tempNode.getNodeValue());
            if (tempNode.hasAttributes()) {
                // get attributes names and values
                NamedNodeMap nodeMap = tempNode.getAttributes();
                for (int i = 0; i < nodeMap.getLength(); i++) {
                    Node node = nodeMap.item(i);
                    System.out.println("attr name : " + node.getNodeName());
                    System.out.println("attr value : " + node.getNodeValue());
                    //    System.out.println("Node Value : " +);
                    if (node.getNodeName() == "class") {
                        Class cls = Class.forName(node.getNodeValue());
                    }
                }
            }
            if (tempNode.hasChildNodes()) {
                // loop again if has child nodes
                printNote(tempNode.getChildNodes());
            }
            System.out.println("Node Name =" + tempNode.getNodeName() + " [CLOSE]");
        }

XML文件看起来像这样:

<ui-model>
<waui>
    <abstract-container wauiId = '1'>
        <abstract-button wauiId = '2'></abstract-button>
        <abstract-button wauiId = '3'></abstract-button>
        <abstract-button wauiId = '4'></abstract-button>
    </abstract-container>
</waui>
<wrm>
    <wr-item wauiId = '2'>
        <abstract-properties>
            <abstract-property name='text'>Button1</abstract-property>
        </abstract-properties>
        <polymorphic-properties>
            <polymorphic-instance piId='swingRectButton'>
                <polymorphic-property name='width'>100</polymorphic-property>
                <polymorphic-property name='height'>50</polymorphic-property>
            </polymorphic-instance>
            <polymorphic-instance piId='swingRoundButton'>
                <polymorphic-property name='radius'>80</polymorphic-property>
                <polymorphic-property name='background-color'>red</polymorphic-property>
            </polymorphic-instance>
        </polymorphic-properties>
    </wr-item>
    <wr-item wauiId = '3'>
        <abstract-properties>
            <abstract-property name='text'>Button2</abstract-property>
        </abstract-properties>
        <polymorphic-properties>
            <polymorphic-instance piId='swingRectButton'>
                <polymorphic-property name='width'>200</polymorphic-property>
                <polymorphic-property name='height'>60</polymorphic-property>
            </polymorphic-instance>
        </polymorphic-properties>
    </wr-item>
    <wr-item wauiId = '4'>
        <abstract-properties>
            <abstract-property name='text'>Button3</abstract-property>
        </abstract-properties>
        <polymorphic-properties>
            <polymorphic-instance piId='swingRoundButton'>
                <polymorphic-property name='radius'>9</polymorphic-property>
                <polymorphic-property name='background-color'>blue</polymorphic-property>
            </polymorphic-instance>
        </polymorphic-properties>
    </wr-item>
</wrm>

<widget name='abstract-button'>
<abstract-properties>
    <property name='text' id='wsl_1'/>
</abstract-properties>
<polymorphic-instances>
    <instance name='swingRectButton'>
        <polymorphic-properties>
            <property name='width' />
            <property name='height' />
        </polymorphic-properties>
    </instance>
    <instance name='swingRoundButton'>
        <property name='radius' />
        <property name='background-color' />
    </instance>
</polymorphic-instances>

<polymorphic-instances-api>
    <polymorphic-instance id='swingRectButton' class='javax.swing.JButton'>
        <property name='text'>
            <native-method>setText</native-method>
            <param-type>String</param-type>
        </property>
        <property name='width'>
            <native-method>setWidth</native-method>
            <param-type>Integer</param-type>
        </property>
        <property name='height'>
            <native-method>setHeight</native-method>
            <param-type>Integer</param-type>
        </property>
    </polymorphic-instance>
    <polymorphic-instance id='swingRoundButton' class='gr.epp.aswing.RoundButton'>
        <property name='text'>
            <native-method>setLabel</native-method>
            <param-type>String</param-type>
        </property>
        <property name='radius'>
            <native-method>setRadius</native-method>
            <param-type>Integer</param-type>
        </property>
        <property name='background-color'>
            <native-method>setBackgroundColor</native-method>
            <param-type>String</param-type>
        </property>
    </polymorphic-instance>
</polymorphic-instances-api>

我想写这篇文章作为对你问题的评论,但经过深思熟虑,我认为这是一个合适的答案。

避免过早优化。

如果你已经编写了可以工作的代码,并且你遇到了一个特定的问题,那么解释这个问题。但是你不应该尝试优化你的代码,除非它有一个可识别的问题。

见http://c2.com/cgi/wiki?PrematureOptimization

相关内容

  • 没有找到相关文章

最新更新