【问题标题】:Add new tag in XML file with different attributes在 XML 文件中添加具有不同属性的新标签
【发布时间】:2015-03-19 15:00:10
【问题描述】:

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

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 中做到这一点?

【问题讨论】:

  • 您能否分享意外的输出,或者您用于添加新行的代码。您共享的代码是编辑现有行,但您的问题是当您尝试添加新行时您的代码不起作用

标签: java xml xml-parsing domparser


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-09-24
    • 1970-01-01
    • 2012-07-31
    • 1970-01-01
    相关资源
    最近更新 更多