gpt4 book ai didi

java - 如何在JAVA中使用JAXB正确生成XML

转载 作者:行者123 更新时间:2023-12-02 05:59:02 25 4
gpt4 key购买 nike

也许我的问题与这些问题类似
How do I parse this XML in Java with JAXB?
我想生成一些 XML 文件来显示一些数据但我在 JAXB 中正确使用一些 @XmlAttribute@XmlValue 时遇到不同的问题
这是 XML 输出应该是:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<AnlKerja>
<Analisa no="1">
<Kode>B.1</Kode>
<Uraian>some description</Uraian>
<Material>
<item type="value">ID|ID|ID</item>
</Material>
<Jumlah>some value</Jumlah>
</Analisa>
<Analisa no="2">
<Kode>B.3</Kode>
<Uraian>some description</Uraian>
<Material>
<item type="value">ID|ID|ID</item>
<item type="value">ID|ID|ID</item>
</Material>
<Jumlah>some value</Jumlah>
</Analisa>
</AnlKerja>

自从我 2 天前开始使用 JAXB我不知道如何正确使用它,对我来说阅读 JAXB 文档指南有点困难因为我的母语不是英语。这是我的 JAXB

的 java 代码

AnlKerja.java

@XmlRootElement(name = "AnlKerja")
public class AnlKerja {
@XmlElementWrapper(name = "Analisa")
@XmlElement(name = "Analisa")
private ArrayList<Analisa> analisa;

public ArrayList<Analisa> getAnl() {
return analisa;
}

public void setAnl(ArrayList<Analisa> anl) {
this.analisa = anl;
}
}

Analisa.java

@XmlRootElement(name="Analisa")
@XmlType(propOrder = {"no","value","kode","uraian","material","jumlah"})
public class Analisa {


@XmlAttribute
private String no;
public String getNo() {
return no;
}

public void setNo(String no) {
this.no = no;
}

@XmlValue
private int value;
public int getValue() {
return value;
}

public void setValue(int value) {
this.value = value;
}

private String kode,uraian;
private double jumlah;

@XmlElement(name="Kode")
public String getKode() {
return kode;
}

public void setKode(String kode) {
this.kode = kode;
}
@XmlElement(name="Uraian")
public String getUraian() {
return uraian;
}

public void setUraian(String uraian) {
this.uraian = uraian;
}
@XmlElementWrapper(name = "material")
private ArrayList<Material> material;
public ArrayList<Material> getMaterial() {
return material;
}

public void setMaterial(ArrayList<Material> material) {
this.material = material;
}
@XmlElement(name="Jumlah")
public double getJumlah() {
return jumlah;
}

public void setJumlah(double jumlah) {
this.jumlah = jumlah;
}
}

Material .java

@XmlRootElement(name = "Material")
public class Material {
@XmlElement(name = "items")
private String items;

public Material() {
this.items = "";
this.tipe = "";
this.value = "";
}
public Material(String[] isi, String tipe, String deskripsi) {
int temp = isi.length;
for (int x=0; x<temp; x++) {
this.items += isi[x]+"|";
};
this.value = deskripsi;
this.tipe = tipe;
}
public String getItems() {
return items;
}

public void setItems(String items) {
this.items = items;
}
public void setItems(String[] items) {
int temp = items.length;
for (int x=0; x<temp; x++) {
this.items += items[x]+"|";
}
}

@XmlAttribute
private String tipe;
public String getTipe() {
return tipe;
}

public void setTipe(String tipe) {
this.tipe = tipe;
}

@XmlValue
private String value;
public String getValue() {
return value;
}

public void setValue(String value) {
this.value = value;
}
}

演示.java

public class Demo {
private static final String STORE_XML = "results/ANL.xml";

public static void main(String[] args) throws JAXBException, IOException{
ArrayList<Material> mtr = new ArrayList<Material>();
ArrayList<Analisa> anl = new ArrayList<Analisa>();
AnlKerja anKerja = new AnlKerja();

Material mat = new Material();
mat.setTipe("tipe");
mat.setValue("Bahan");
mat.setItems("MT001|MT002|MT003");
mtr.add(mat);
mat.setTipe("tipe");
mat.setValue("Tenaga");
mat.setItems("MT0001|MT0002|MT0003");
mtr.add(mat);

Analisa ana = new Analisa();
ana.setNo("no");
ana.setValue(1);
ana.setKode("B.1");
ana.setUraian("some description");
ana.setMaterial(mtr);
ana.setJumlah(122414.03);
anl.add(ana);

anKerja.setAnl(anl);

JAXBContext jCont = JAXBContext.newInstance(AnlKerja.class);
Marshaller marshal = jCont.createMarshaller();
marshal.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);

marshal.marshal(anKerja, System.out);

marshal.marshal(anKerja, new File(STORE_XML));
}
}

这是我得到的错误

Exception in thread "main" com.sun.xml.internal.bind.v2.runtime.IllegalAnnotationsException: 8 counts of IllegalAnnotationExceptions
If a class has @XmlElement property, it cannot have @XmlValue property.
this problem is related to the following location:
at private int XML.Analisa.value
at XML.Analisa
at private java.util.ArrayList XML.AnlKerja.analisa
at XML.AnlKerja
this problem is related to the following location:
at public int XML.Analisa.getValue()
at XML.Analisa
at private java.util.ArrayList XML.AnlKerja.analisa
at XML.AnlKerja
If a class has @XmlElement property, it cannot have @XmlValue property.
this problem is related to the following location:
at private java.lang.String XML.Material.value
at XML.Material
at private java.util.ArrayList XML.Analisa.material
at XML.Analisa
at private java.util.ArrayList XML.AnlKerja.analisa
at XML.AnlKerja
this problem is related to the following location:
at public java.lang.String XML.Material.getValue()
at XML.Material
at private java.util.ArrayList XML.Analisa.material
at XML.Analisa
at private java.util.ArrayList XML.AnlKerja.analisa
at XML.AnlKerja
Class has two properties of the same name "material"
this problem is related to the following location:
at public java.util.ArrayList XML.Analisa.getMaterial()
at XML.Analisa
at private java.util.ArrayList XML.AnlKerja.analisa
at XML.AnlKerja
this problem is related to the following location:
at private java.util.ArrayList XML.Analisa.material
at XML.Analisa
at private java.util.ArrayList XML.AnlKerja.analisa
at XML.AnlKerja
Class has two properties of the same name "no"
this problem is related to the following location:
at public java.lang.String XML.Analisa.getNo()
at XML.Analisa
at private java.util.ArrayList XML.AnlKerja.analisa
at XML.AnlKerja
this problem is related to the following location:
at private java.lang.String XML.Analisa.no
at XML.Analisa
at private java.util.ArrayList XML.AnlKerja.analisa
at XML.AnlKerja
Class has two properties of the same name "value"
this problem is related to the following location:
at public int XML.Analisa.getValue()
at XML.Analisa
at private java.util.ArrayList XML.AnlKerja.analisa
at XML.AnlKerja
this problem is related to the following location:
at private int XML.Analisa.value
at XML.Analisa
at private java.util.ArrayList XML.AnlKerja.analisa
at XML.AnlKerja
Class has two properties of the same name "items"
this problem is related to the following location:
at public java.lang.String XML.Material.getItems()
at XML.Material
at private java.util.ArrayList XML.Analisa.material
at XML.Analisa
at private java.util.ArrayList XML.AnlKerja.analisa
at XML.AnlKerja
this problem is related to the following location:
at private java.lang.String XML.Material.items
at XML.Material
at private java.util.ArrayList XML.Analisa.material
at XML.Analisa
at private java.util.ArrayList XML.AnlKerja.analisa
at XML.AnlKerja
Class has two properties of the same name "tipe"
this problem is related to the following location:
at public java.lang.String XML.Material.getTipe()
at XML.Material
at private java.util.ArrayList XML.Analisa.material
at XML.Analisa
at private java.util.ArrayList XML.AnlKerja.analisa
at XML.AnlKerja
this problem is related to the following location:
at private java.lang.String XML.Material.tipe
at XML.Material
at private java.util.ArrayList XML.Analisa.material
at XML.Analisa
at private java.util.ArrayList XML.AnlKerja.analisa
at XML.AnlKerja
Class has two properties of the same name "value"
this problem is related to the following location:
at public java.lang.String XML.Material.getValue()
at XML.Material
at private java.util.ArrayList XML.Analisa.material
at XML.Analisa
at private java.util.ArrayList XML.AnlKerja.analisa
at XML.AnlKerja
this problem is related to the following location:
at private java.lang.String XML.Material.value
at XML.Material
at private java.util.ArrayList XML.Analisa.material
at XML.Analisa
at private java.util.ArrayList XML.AnlKerja.analisa
at XML.AnlKerja

最佳答案

  1. 这是 XML,而不是 XML 架构
  2. 您的 XML 文件中有一些错误:标记项在某些行上未关闭。修复此问题后,您可以在一项在线服务上从 XML 创建 XSD,例如:https://www.freeformatter.com/xsd-generator.html
  3. 通过 XSD 文件,您可以使用 JDK 中的 xjc 实用程序 ( https://docs.oracle.com/javase/8/docs/technotes/tools/unix/xjc.html ) 创建 JAXB 绑定(bind)类

更新生成的类:

//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, v2.2.8-b130911.1802
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
// Any modifications to this file will be lost upon recompilation of the source schema.
// Generated on: 2019.05.05 at 08:59:07 PM MSK
//


package generated;

import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.XmlValue;


/**
* <p>Java class for anonymous complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* &lt;complexType>
* &lt;complexContent>
* &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* &lt;sequence>
* &lt;element name="Analisa" maxOccurs="unbounded" minOccurs="0">
* &lt;complexType>
* &lt;complexContent>
* &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* &lt;sequence>
* &lt;element name="Kode" type="{http://www.w3.org/2001/XMLSchema}string"/>
* &lt;element name="Uraian" type="{http://www.w3.org/2001/XMLSchema}string"/>
* &lt;element name="Material">
* &lt;complexType>
* &lt;complexContent>
* &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* &lt;sequence>
* &lt;element name="item" maxOccurs="unbounded" minOccurs="0">
* &lt;complexType>
* &lt;simpleContent>
* &lt;extension base="&lt;http://www.w3.org/2001/XMLSchema>string">
* &lt;attribute name="type" type="{http://www.w3.org/2001/XMLSchema}string" />
* &lt;/extension>
* &lt;/simpleContent>
* &lt;/complexType>
* &lt;/element>
* &lt;/sequence>
* &lt;/restriction>
* &lt;/complexContent>
* &lt;/complexType>
* &lt;/element>
* &lt;element name="Jumlah" type="{http://www.w3.org/2001/XMLSchema}string"/>
* &lt;/sequence>
* &lt;attribute name="no" type="{http://www.w3.org/2001/XMLSchema}byte" />
* &lt;/restriction>
* &lt;/complexContent>
* &lt;/complexType>
* &lt;/element>
* &lt;/sequence>
* &lt;/restriction>
* &lt;/complexContent>
* &lt;/complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"analisa"
})
@XmlRootElement(name = "AnlKerja")
public class AnlKerja {

@XmlElement(name = "Analisa")
protected List<AnlKerja.Analisa> analisa;

/**
* Gets the value of the analisa property.
*
* <p>
* This accessor method returns a reference to the live list,
* not a snapshot. Therefore any modification you make to the
* returned list will be present inside the JAXB object.
* This is why there is not a <CODE>set</CODE> method for the analisa property.
*
* <p>
* For example, to add a new item, do as follows:
* <pre>
* getAnalisa().add(newItem);
* </pre>
*
*
* <p>
* Objects of the following type(s) are allowed in the list
* {@link AnlKerja.Analisa }
*
*
*/
public List<AnlKerja.Analisa> getAnalisa() {
if (analisa == null) {
analisa = new ArrayList<AnlKerja.Analisa>();
}
return this.analisa;
}


/**
* <p>Java class for anonymous complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* &lt;complexType>
* &lt;complexContent>
* &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* &lt;sequence>
* &lt;element name="Kode" type="{http://www.w3.org/2001/XMLSchema}string"/>
* &lt;element name="Uraian" type="{http://www.w3.org/2001/XMLSchema}string"/>
* &lt;element name="Material">
* &lt;complexType>
* &lt;complexContent>
* &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* &lt;sequence>
* &lt;element name="item" maxOccurs="unbounded" minOccurs="0">
* &lt;complexType>
* &lt;simpleContent>
* &lt;extension base="&lt;http://www.w3.org/2001/XMLSchema>string">
* &lt;attribute name="type" type="{http://www.w3.org/2001/XMLSchema}string" />
* &lt;/extension>
* &lt;/simpleContent>
* &lt;/complexType>
* &lt;/element>
* &lt;/sequence>
* &lt;/restriction>
* &lt;/complexContent>
* &lt;/complexType>
* &lt;/element>
* &lt;element name="Jumlah" type="{http://www.w3.org/2001/XMLSchema}string"/>
* &lt;/sequence>
* &lt;attribute name="no" type="{http://www.w3.org/2001/XMLSchema}byte" />
* &lt;/restriction>
* &lt;/complexContent>
* &lt;/complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"kode",
"uraian",
"material",
"jumlah"
})
public static class Analisa {

@XmlElement(name = "Kode", required = true)
protected String kode;
@XmlElement(name = "Uraian", required = true)
protected String uraian;
@XmlElement(name = "Material", required = true)
protected AnlKerja.Analisa.Material material;
@XmlElement(name = "Jumlah", required = true)
protected String jumlah;
@XmlAttribute(name = "no")
protected Byte no;

/**
* Gets the value of the kode property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getKode() {
return kode;
}

/**
* Sets the value of the kode property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setKode(String value) {
this.kode = value;
}

/**
* Gets the value of the uraian property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getUraian() {
return uraian;
}

/**
* Sets the value of the uraian property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setUraian(String value) {
this.uraian = value;
}

/**
* Gets the value of the material property.
*
* @return
* possible object is
* {@link AnlKerja.Analisa.Material }
*
*/
public AnlKerja.Analisa.Material getMaterial() {
return material;
}

/**
* Sets the value of the material property.
*
* @param value
* allowed object is
* {@link AnlKerja.Analisa.Material }
*
*/
public void setMaterial(AnlKerja.Analisa.Material value) {
this.material = value;
}

/**
* Gets the value of the jumlah property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getJumlah() {
return jumlah;
}

/**
* Sets the value of the jumlah property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setJumlah(String value) {
this.jumlah = value;
}

/**
* Gets the value of the no property.
*
* @return
* possible object is
* {@link Byte }
*
*/
public Byte getNo() {
return no;
}

/**
* Sets the value of the no property.
*
* @param value
* allowed object is
* {@link Byte }
*
*/
public void setNo(Byte value) {
this.no = value;
}


/**
* <p>Java class for anonymous complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* &lt;complexType>
* &lt;complexContent>
* &lt;restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* &lt;sequence>
* &lt;element name="item" maxOccurs="unbounded" minOccurs="0">
* &lt;complexType>
* &lt;simpleContent>
* &lt;extension base="&lt;http://www.w3.org/2001/XMLSchema>string">
* &lt;attribute name="type" type="{http://www.w3.org/2001/XMLSchema}string" />
* &lt;/extension>
* &lt;/simpleContent>
* &lt;/complexType>
* &lt;/element>
* &lt;/sequence>
* &lt;/restriction>
* &lt;/complexContent>
* &lt;/complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"item"
})
public static class Material {

protected List<AnlKerja.Analisa.Material.Item> item;

/**
* Gets the value of the item property.
*
* <p>
* This accessor method returns a reference to the live list,
* not a snapshot. Therefore any modification you make to the
* returned list will be present inside the JAXB object.
* This is why there is not a <CODE>set</CODE> method for the item property.
*
* <p>
* For example, to add a new item, do as follows:
* <pre>
* getItem().add(newItem);
* </pre>
*
*
* <p>
* Objects of the following type(s) are allowed in the list
* {@link AnlKerja.Analisa.Material.Item }
*
*
*/
public List<AnlKerja.Analisa.Material.Item> getItem() {
if (item == null) {
item = new ArrayList<AnlKerja.Analisa.Material.Item>();
}
return this.item;
}


/**
* <p>Java class for anonymous complex type.
*
* <p>The following schema fragment specifies the expected content contained within this class.
*
* <pre>
* &lt;complexType>
* &lt;simpleContent>
* &lt;extension base="&lt;http://www.w3.org/2001/XMLSchema>string">
* &lt;attribute name="type" type="{http://www.w3.org/2001/XMLSchema}string" />
* &lt;/extension>
* &lt;/simpleContent>
* &lt;/complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"value"
})
public static class Item {

@XmlValue
protected String value;
@XmlAttribute(name = "type")
protected String type;

/**
* Gets the value of the value property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getValue() {
return value;
}

/**
* Sets the value of the value property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setValue(String value) {
this.value = value;
}

/**
* Gets the value of the type property.
*
* @return
* possible object is
* {@link String }
*
*/
public String getType() {
return type;
}

/**
* Sets the value of the type property.
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setType(String value) {
this.type = value;
}

}

}

}

}

关于java - 如何在JAVA中使用JAXB正确生成XML,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55990007/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com