使用类信息解析XML并通过Reflection实例化

时间:2014-06-10 18:05:22

标签: java xml dom reflection xml-parsing

我有两个XML文件,其中包含有关某些类的信息。在解析XML之后,我想通过Reflection实例化这些类。

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

我真的想不出别的什么期待很多IF ... ELSE( 像这样:

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>

1 个答案:

答案 0 :(得分:1)

我考虑过将此作为对您问题的评论,但经过深思熟虑后,我认为这是一个合适的答案。

避免过早优化。

如果您已经编写了有效的代码并且遇到了特定问题,那么请解释该问题。但是,除非存在可识别的问题,否则不应尝试优化代码。

请参阅http://c2.com/cgi/wiki?PrematureOptimization