在XML文件中添加具有不同属性的新标记



我正在尝试添加一行具有不同属性的标签"。我可以编辑现有的行,但不能添加具有不同属性的新标记。

XML文件

   <elementDefinitionPackage elementDefinitionPackageName="kohler-decisionmaker" elementDefinitionPackageVersion="3.2.0.42" elementLibraryFilename="libkohler-decisionmaker.so" elementLibraryVersion="3.2.0.42" minSupportedGDDVersion="1.0" minSupportedUMGFirmwareVersion="1.1.0.0" xmlns="http://xmlns.commonplatform.avocent.com/mss/ddt/template" xmlns:cm="http://xmlns.commonplatform.avocent.com/mss/ddt/common" xmlns:r="http://xmlns.commonplatform.avocent.com/mss/ddt/rules" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.commonplatform.avocent.com/mss/ddt/template XSD_ddt.xsd         http://xmlns.commonplatform.avocent.com/mss/ddt/rules XSD_rules.xsd         http://xmlns.commonplatform.avocent.com/mss/ddt/common XSD_common.xsd">
<elementDefinitionModel manufacturerInSymbol="KOHLER" minSupportedVersionInSymbol="1.4" modelInSymbol="DEC4000" modelQualifierInSymbol="DEC4000" symbolTag="KOHLERDEC4000DEC4000">
    <supportedprotocols>
        <supportedProtocol isSubscribable="false" protocolName="MODBUS/RS-485">
            <properties>
                <property category="EXTCOMM" defaultValue="502" definition="PORT" valueType="Integer" />
                <property category="EXTCOMM" defaultValue="60" definition="TIMEOUT" valueType="Integer" />
                <property category="EXTCOMM" defaultValue="1" definition="SLAVEID" valueType="Integer"/>
            </properties>
            <datapoints>
                <datapoint division="COLL_COMP_CEP" nature="PARAMETRIC" programmaticName="t_val_calc_enrg_interval"/>
            </datapoints>
            <events>
                <event address="3.{SLAVEID}.40259.1" programmaticName="t_evt_gen_mainTankAlarm" values="a:1 i:0" />
            </events>
            <commands>
                <command access="RW" address="#RWDA 3.{SLAVEID}.61105 6.{SLAVEID}.61105" division="CONTROL" nature="ENUM" programmaticName="t_st_gen_setControl" valueTypeInDevice="DATA_POINT_VALUE_TYPE_INTEGER" />
            </commands>
        </supportedProtocol>
    </supportedprotocols>
    <rules>
    </rules>
        <similarModels>
            <similarModel manufacturerInSymbol="KOHLER" minSupportedVersionInSymbol="1.4" modelInSymbol="KD440" modelQualifierInSymbol="KD440" symbolTag="KOHLERKD440KD440"/>
        </similarModels>
</elementDefinitionModel>

预期输出XML文件

      <elementDefinitionPackage elementDefinitionPackageName="kohler-decisionmaker" elementDefinitionPackageVersion="3.2.0.42" elementLibraryFilename="libkohler-decisionmaker.so" elementLibraryVersion="3.2.0.42" minSupportedGDDVersion="1.0" minSupportedUMGFirmwareVersion="1.1.0.0" xmlns="http://xmlns.commonplatform.avocent.com/mss/ddt/template" xmlns:cm="http://xmlns.commonplatform.avocent.com/mss/ddt/common" xmlns:r="http://xmlns.commonplatform.avocent.com/mss/ddt/rules" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://xmlns.commonplatform.avocent.com/mss/ddt/template XSD_ddt.xsd         http://xmlns.commonplatform.avocent.com/mss/ddt/rules XSD_rules.xsd         http://xmlns.commonplatform.avocent.com/mss/ddt/common XSD_common.xsd">
<elementDefinitionModel manufacturerInSymbol="KOHLER" minSupportedVersionInSymbol="1.4" modelInSymbol="DEC4000" modelQualifierInSymbol="DEC4000" symbolTag="KOHLERDEC4000DEC4000">
    <supportedprotocols>
        <supportedProtocol isSubscribable="false" protocolName="MODBUS/RS-485">
            <properties>
                <property category="EXTCOMM" defaultValue="502" definition="PORT" valueType="Integer" />
                <property category="EXTCOMM" defaultValue="60" definition="TIMEOUT" valueType="Integer" />
                <property category="EXTCOMM" defaultValue="1" definition="SLAVEID" valueType="Integer"/>
            </properties>
            <datapoints>
                <datapoint division="COLL_COMP_CEP" nature="PARAMETRIC" programmaticName="t_val_calc_enrg_interval"/>
            </datapoints>
            <events>
                <event address="3.{SLAVEID}.40259.1" programmaticName="t_evt_gen_mainTankAlarm" values="a:1 i:0" />
            </events>
            <commands>
                <command access="RW" address="#RWDA 3.{SLAVEID}.61105 6.{SLAVEID}.61105" division="CONTROL" nature="ENUM" programmaticName="t_st_gen_setControl" valueTypeInDevice="DATA_POINT_VALUE_TYPE_INTEGER" />
            </commands>
        </supportedProtocol>
    </supportedprotocols>
    <rules>
    </rules>
        <similarModels>
            <similarModel manufacturerInSymbol="KOHLER" minSupportedVersionInSymbol="1.4" modelInSymbol="KD440" modelQualifierInSymbol="KD440" symbolTag="KOHLERKD440KD440"/>
            <similarModel manufacturerInSymbol="KOHLER" minSupportedVersionInSymbol="1.4" modelInSymbol="A" modelQualifierInSymbol="B" symbolTag="C"/>
        </similarModels>
</elementDefinitionModel>

Java代码

    private static void updat() {
    NodeList similarmodels = doc.getElementsByTagName("similarModels");
    Element model = null;
    for(int i=0; i<similarmodels.getLength();i++){
        model = (Element) similarmodels.item(i);
        String model = model.getElementsByTagName("similarModel").item(0).getFirstChild().getNodeValue();
        if(model.equalsIgnoreCase("KD440")){
            model.setAttribute("A");
        }else{
            model.setAttribute("B");
        }
    }
}

JAXB&xsd??

无论是DOM还是JAXB都应该没有问题。在JAXB中,您应该能够简单地创建一个新的SimilarModel对象,然后可以将其添加到SimilarModels对象的列表中。

相关内容

  • 没有找到相关文章

最新更新