Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从对象创建复杂xml文件的最简单方法_Java_Xml - Fatal编程技术网

Java 从对象创建复杂xml文件的最简单方法

Java 从对象创建复杂xml文件的最简单方法,java,xml,Java,Xml,从对象中创建复杂xml文件的最佳选项是什么?根文件最多有6个子文件,其中包含大量条目。哪些标记具有设置的值在不同的文件之间可能有所不同 <root> <child> <Child> <child> <child</child> StaX、DOMParser或使用模板。我喜欢将这样的内容传递给对象本身 您可以编写如下界面: public interface XMLWriteable

从对象中创建复杂xml文件的最佳选项是什么?根文件最多有6个子文件,其中包含大量条目。哪些标记具有设置的值在不同的文件之间可能有所不同

<root>
<child>
    <Child>
        <child>
            <child</child>

StaX、DOMParser或使用模板。

我喜欢将这样的内容传递给对象本身

您可以编写如下界面:

public interface XMLWriteable {

   /** Write stuff to XML file **/
   public void writeXML( PrintWriter pWriter );
}
在你们班上:

public class Object2XML implements XMLWriteable{
  ...
  public void writeXML( PrintWriter pWriter ){
    pWriter.println("<ObjectTag>");
    //Write Some values
    pWriter.println("<stringTag>"+value+"</stringTag>");
    //Write other complex XMLWriteable Objects
    XMLWObj.writeXML(pWriter);
    //Close your tag
    pWriter.println("</ObjectTag>");
  }
 }

这是一种可能的方法。希望我能帮上忙。

您可以使用Java体系结构进行XML绑定JAXB API。 我编写了一个简单的employee类来解释如何使用JAXB实现它

/*Employee class as root node*/
@XmlRootElement
public class Employee {

private String name;
private int age;
private int id;
private double salary;

public Employee() {
    super();
    // TODO Auto-generated constructor stub
}

public Employee(String name, int age, int id, double salary) {
    super();
    this.name = name;
    this.age = age;
    this.id = id;
    this.salary = salary;
}

/*add as attribute into root node*/
@XmlAttribute
public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

/*add as element*/
@XmlElement
public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}

@XmlAttribute
public int getId() {
    return id;
}

public void setId(int id) {
    this.id = id;
}

@XmlElement
public double getSalary() {
    return salary;
}

public void setSalary(double salary) {
    this.salary = salary;
}

}
在main方法中,您可以将此员工对象数据写入xml文件

public static void main(String[] args) throws JAXBException,
        FileNotFoundException {

    JAXBContext contextObj = JAXBContext.newInstance(Employee.class);

    Marshaller marshallerObj = contextObj.createMarshaller();
    marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    Employee emp1 = new Employee("saman", 23, 001, 2500.00);

    marshallerObj.marshal(emp1, new FileOutputStream("employee.xml"));

}
employee.xml文件如下所示:-

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
   <employee id="1" name="saman">
    <age>23</age>
    <salary>2500.0</salary>
   </employee>

因此,您可以使用java对象的注释来构建xml文件。格式

这取决于你对最简单的定义。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
   <employee id="1" name="saman">
    <age>23</age>
    <salary>2500.0</salary>
   </employee>