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

时间:2015-03-19 15:00:10

标签: java xml xml-parsing domparser

我正在尝试使用不同的属性添加新的标记行“”。我可以编辑现有的行,但无法添加具有不同属性的新标记。

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&amp; XSD ??

1 个答案:

答案 0 :(得分:0)

既不是DOM也不是JAXB应该没问题。在JAXB中,您应该只需创建一个新的SimilarModel对象,然后可以将其添加到SimilarModels对象的列表中。