简体   繁体   English

Java从XML文件获取属性并创建对象

[英]Java Get Properties from XML File and Create Object

I have a xml file like this: 我有一个这样的xml文件:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ActionDictionary>
    <action id="100000">
        <actiontype>BackupFile</actiontype>
        <filename>translated.properties</filename>
    </action>

    <action id="100001">
        <actiontype>ModifyPropertiesFile</actiontype>
        <filename>translated.properties</filename>
    </action>


    <action id="100002">
        <actiontype>DeployDomain</actiontype>
        <hostname>localhost</hostname>
        <port>8000</port>
        <domainhome>c:\\domain</domainhome>
    </action>   
</ActionDictionary> 

When executing, an action id will be give to the program (ex: id="10000"). 执行时,将向该程序提供一个动作ID(例如:id =“ 10000”)。 The program will use the id to get all properties of the action tag in the xml file above which have the same id with the given id. 程序将使用id获取上面xml文件中action标签的所有属性,这些属性具有与给定ID相同的ID。 These properties could be put to somewhere like an object. 这些属性可以放在对象之类的地方。 Then I could use this object to continue the other business. 然后,我可以使用该对象继续其他事务。

How could I implement this in Java? 如何用Java实现呢?

@Hoang, you can use javax.xml.bind.annotation.XmlElement and javax.xml.bind.annotation.XmlType of Javax api. @Hoang,可以使用Javax api的javax.xml.bind.annotation.XmlElement和javax.xml.bind.annotation.XmlType。

I would suggest to have a read through api docs Javax API 我建议您通读api docs Javax API

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM