【问题标题】:update data in java class as per change in attribute in the xml file根据 xml 文件中属性的更改更新 java 类中的数据
【发布时间】:2012-04-04 14:40:05
【问题描述】:

我想给xml定义文件添加一个属性

现在我希望这个更改反映在 java 类中。你能建议如何做到吗?我还想将此属性作为数据成员添加到一个带有它的java类中;getter和setter。我已经做到了。我想知道如何将 xml 节点中的值分配给此类中的 java 属性。请告诉我唯一的逻辑。

【问题讨论】:

  • 只是为了澄清:你想改变源代码还是编译的类文件?我不清楚。
  • Andreas 我只是将值从 xml 的节点分配到 java 属性中的方法 public void init(Node node) 。你能帮我解决这个问题吗?请注意,java 类的成员与 xml 文件中的属性同步。那么请告诉我如何将节点中的值分配到java类中的相应属性中。
  • 基本上有一个xsd文件。我想向这个 xsd 文件添加一个属性。现在这个变化应该反映在一个 java 类 dataservices.DataServicesLayout.java 添加新常量,比如 ex:- public static final String SERVICE ID= "serviceId" 。第二个目标是在另一个 java 类中添加属性 private String serviceId 及其 getter 和 setter 方法。然后我们需要将xml文件的节点的值赋值给java属性到方法public void init(Node node)

标签: java xml


【解决方案1】:

由于您已经有 xml 文件的架构并且您希望数据类型使用 java 类,因此请考虑使用JAXB。这个 xml 绑定 API 可以从模式中自动生成类,它提供了方便的方法来编组和解组 XML 文档。 (IAW:“将 XML 转换为 java 实例,反之亦然)。

【讨论】:

  • 我只希望逻辑将 xml 节点中的值分配到 java 属性中的方法中。我整理的其他要求。你能在这方面提供帮助吗?
  • 没有简单的“逻辑”。如果您想自动执行此操作 - 使用 JAXB 或最初自动生成类的框架并简单地重新运行自动生成。如果您想手动进行 - 分析架构,然后是源文件,最后是绑定规则/逻辑,然后编写字段和方法。
【解决方案2】:

尝试使用此代码实现

你的属性.xml

<attributes>
    <attribute-list>
            <attribute>
                <fname>riddhish</fname>
            <lname>chaudhari</lname>
        </attribute>
    </attribute-list>
<attributes>

类文件

public static final String ATTRIBUTE_LIST = "ATTRIBUTE_LIST";
public static final String ATTRIBUTE = "ATTRIBUTE";
public static final String FNAME = "FNAME";

xml 文件中的评分属性代码

Document document = null;
NodeList nodeList = null;
Node node = null;

nodeList = document.getElementsByTagName("----file attributes.xml---").item(0).getChildNodes();
HashMap <String,Object> localParameterMap  = new HashMap<String,Object>();

for(int i=0; i<nodeList.getLength(); i++){
    node = nodeList.item(i);
    if(node.getNodeName().equals("attribute-list")){
        Collection objCollection = readAttributeList(node);
        localParameterMap.put(ATTRIBUTE_LIST, objCollection);
    }
}

function() 读取属性列表

private Collection readAttributeList(Node node){
    Collection<Object> objCollection = new ArrayList<Object>();
    NodeList nodeList = node.getChildNodes();   

    for(int i=0; i < nodeList.getLength(); i++){
        Node subNode = nodeList.item(i);

        if(subNode.getNodeName().equals("attribute")){
            NodeList attributeList = subNode.getChildNodes();
            HashMap <String,Object> attributeMap  = new HashMap<String,Object>();

                for(int j=0; j<attributeList.getLength(); j++){
                    Node attributeNode = attributeList.item(j);

                    if(attributeNode.getNodeName().equals("fname")){
                        attributeMap.put(FNAME, attributeNode.getTextContent().trim());
                    }   
                }
        }
        objCollection.add(attributeMap);
    }
    return objCollection;
}

用于读取变量中的属性值

String strfname = null;

if(map.get(CLASS_NAME.FNAME) != null) {
    strfname = (String)map.get(CLASS_NAME.FNAME);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-14
    • 2016-01-07
    • 1970-01-01
    • 1970-01-01
    • 2014-02-18
    • 2023-01-06
    • 1970-01-01
    • 2012-04-16
    相关资源
    最近更新 更多