Одна из частей моего проекта по окончанию учебы состоит в том, чтобы проанализировать сложный файл XML (включающий элемент, атрибут, значение) и отобразить его в интерфейсе, принимая во внимание атрибуты каждого тега. Пример:
<Field description = "Device installation address" align = "left" style = "normal" newline = "true"> TAG_DEVICE_INSTALLATION_ADDRESS </Field>
На дисплее значение должно быть слева с обычным стилем и вернуться к строке \ n
У меня две проблемы:
при разборе с jaxb существуют некоторые формы, где API не может его проанализировать, несмотря на то, что я определяю все возможности с другой аннотацией jaxb (пример:
<Field description="TIN">
<Label align="left" style="normal" newline="true" column="0">metier</Label>
<Value align="left" style="normal" newline="false" column="1">Sport</Value>
</Field>
RQ: я тестирую сгенерированные классы с xj c дает команду на получение, классы фабрики объектов, но тот же результат: /
Для отображения я могу использовать текстовую область, но я могу настроить текстовую строку на строку, должен Я изменил свой метод?
Мой код:
<?xml version="1.0" encoding="UTF-8"?>
<Receipt>
<Field description="Name of the taxpayer" align="left" style="normal" newline="true" repeat = "true">TAG_TAXPAYER_NAME</Field>
<Field description="Device installation address" align="left" style="normal" repeat = "true">TAG_DEVICE_INSTALLATION_ADDRESS</Field>
<Field description="TIN">
<Label align="left" style="normal" newline="true" column="0">STR_TIN</Label>
<Value align="left" style="normal" newline="true" column="1">TAG_TIN</Value>
</Field>
<Field description="Registration number of the cash register">
<Label align="left" style="normal" newline="true" column="0">STR_RN</Label>
<Value align="left" style="normal" newline="true" column="1">TAG_RN</Value>
</Field>
</Receipt>
Класс квитанции:
package appjaxb1.tel;
import java.util.ArrayList;
import java.util.List;
import javax.activation.DataHandler;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttachmentRef;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.*;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"field","label","value"
})
@XmlRootElement(name = "Receipt")
public class Receipt {
@XmlElementRef(name = "Field", required = true)
protected List<Field> field;
@XmlElement(name = "Label",type= Label.class , required = false)
protected List<Label> label;
@XmlElement(name = "Value",type= Value.class, required = false)
protected List<Value> value;
public List<Label> getLabel() {
if (label == null) {
label = new ArrayList<Label>();
}
return this.label;
}
public List<Value> getValue() {
if (value == null) {
value = new ArrayList<Value>();
}
return this.value;
}
public List<Field> getField() {
if (field == null) {
field = new ArrayList<Field>();
}
return this.field;
}
}
Поле класса:
package appjaxb1.tel;
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.XmlAnyAttribute;
import javax.xml.bind.annotation.XmlAnyElement;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementRef;
import javax.xml.bind.annotation.XmlElementRefs;
import javax.xml.bind.annotation.XmlElements;
import javax.xml.bind.annotation.XmlMixed;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlSchemaType;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.adapters.CollapsedStringAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
import javax.xml.bind.annotation.XmlValue;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {
"content"
})
@XmlRootElement(name = "Field")
public class Field {
@XmlElementRefs({
@XmlElementRef(name = "Label", type = Label.class, required = false),
@XmlElementRef(name = "Value", type = Value.class, required = false)
})
@XmlMixed
protected List<Object> content;
@XmlAttribute(name = "align")
//@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
@XmlSchemaType(name = "string")
protected String align;
@XmlAttribute(name = "description", required = true)
@XmlSchemaType(name = "string")
protected String description;
@XmlAttribute(name = "newline")
protected Boolean newline;
@XmlAttribute(name = "repeat")
protected Boolean repeat;
@XmlAttribute(name = "style")
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
@XmlSchemaType(name = "string")
protected String style;
public List<Object> getContent() {
if (content == null) {
content = new ArrayList<Object>();
}
return this.content;
}
public String getAlign() {
return align;
}
public void setAlign(String value) {
this.align = value;
}
public String getDescription() {
return description;
}
public void setDescription(String value) {
this.description = value;
}
public Boolean isNewline() {
return newline;
}
public void setNewline(Boolean value) {
this.newline = value;
}
public Boolean isRepeat() {
return repeat;
}
public void setRepeat(Boolean value) {
this.repeat = value;
}
public String getStyle() {
return style;
}
public void setStyle(String value) {
this.style = value;
}
}
Метка класса:
package appjaxb1.tel;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlSchemaType;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.XmlValue;
import javax.xml.bind.annotation.adapters.CollapsedStringAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {"value"})
@XmlRootElement(name = "Label")
public class Label {
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
@XmlSchemaType(name = "string")
@XmlValue
protected String value;
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
@XmlSchemaType(name = "string")
@XmlAttribute(name = "alignn", required = true)
protected String alignn;
@XmlAttribute(name = "column", required = true)
protected int column;
@XmlAttribute(name = "newline", required = true)
protected boolean newline;
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
@XmlSchemaType(name = "string")
@XmlAttribute(name = "style", required = true)
protected String style;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getAlignn() {
return alignn;
}
public void setAlign(String value) {
this.alignn = value;
}
public int getColumn() {
return column ;
}
public void setColumn(int value) {
this.column = value;
}
public boolean isNewline() {
return newline;
}
public void setNewline(boolean value) {
this.newline = value;
}
public String getStyle() {
return style;
}
public void setStyle(String value) {
this.style = value;
}
}
Значение
package appjaxb1.tel;
import java.math.BigInteger;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlSchemaType;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.XmlValue;
import javax.xml.bind.annotation.adapters.CollapsedStringAdapter;
import javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = {"value"
})
@XmlRootElement(name = "Value")
public class Value {
@XmlValue
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
@XmlSchemaType(name = "String")
protected String value;
@XmlAttribute(name = "align", required = true)
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
@XmlSchemaType(name = "String")
protected String align;
@XmlAttribute(name = "column", required = true)
protected BigInteger column;
@XmlAttribute(name = "newline", required = true)
protected boolean newline;
@XmlAttribute(name = "style", required = true)
@XmlJavaTypeAdapter(CollapsedStringAdapter.class)
@XmlSchemaType(name = "String")
protected String style;
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getAlign() {
return align;
}
public void setAlign(String value) {
this.align = value;
}
public BigInteger getColumn() {
return column;
}
public void setColumn(BigInteger value) {
this.column = value;
}
public boolean isNewline() {
return newline;
}
public void setNewline(boolean value) {
this.newline = value;
}
public String getStyle() {
return style;
}
public void setStyle(String value) {
this.style = value;
}
}
Класс теста:
package appjaxb1.tel;
import java.io.File;
import java.util.ArrayList;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.stream.StreamSource;
public class Test {
public static void main(String[] args) {
try {
JAXBContext jc = JAXBContext.newInstance("appjaxb1.tel");
Unmarshaller unmarshaller = jc.createUnmarshaller();
Receipt receipt = (Receipt) unmarshaller.unmarshal(new File("C:\\Users\\DELL\\Desktop\\terminall\\appjaxb1\\src\\appjaxb1\\tel\\NewFile.xml"));
System.out.println("******************");
ArrayList<Field> listF = (ArrayList<appjaxb1.tel.Field>) receipt.getField() ;
Label label =new Label();
Value value = new Value ();
System.out.println(listF.toString());
for (int i = 0; i < listF.size() ; i++) {
Field field = (Field) listF.get(i);
//test 1
System.out.println("Field ");
if(field.description!= null)
System.out.println(" Description : " + field.getDescription() );
else System.out.print("");
if(field.align != null) {
System.out.println("Align Field : " + field.getAlign());
if(field.style != null)
System.out.println("Style Field : " + field.getStyle());
else System.out.print("");
if(field.newline.toString() != null)
System.out.println("New Line or Not : " + field.isNewline());
else System.out.print("");
if(field.repeat.toString() != null)
System.out.println("Repet or not " + field.isRepeat());
else System.out.print(")");
if(field.content == null)
System.out.print(" ");
else System.out.println("Contenu : " + field.getContent());
}
else {
//System.out.println(" Description : " + field.getDescription() );
// label exist
//if(label.alignn != null)
System.out.println("///////////////");
System.out.println(receipt.getLabel());
System.out.println(receipt.label.add(label));
System.out.println(" Label : " + "Align Label :" + label.getAlignn());
//else System.out.print(" ");
//if(label.style != null)
System.out.println(" Label : " + "Align Label :" + label.getStyle());
//else System.out.print(" ");
System.out.println(" Label : " + "New line Label :" + label.isNewline());
System.out.println(" Label : " + "Column Label :" + label.getColumn());
System.out.println(" Label : " + "Valeur Label :" + label.getValue());
// value
//if(value.align !=null)
System.out.println(" value : " + "Align value :" + value.getAlign());
//else System.out.print(" ");
//if(value.style != null)
System.out.println(" value : " + "Align value :" + value.getStyle());
//else System.out.print(" ");
System.out.println(" value : " + "New line value :" + value.isNewline());
//if(value.column.toString() != null )
System.out.println(" value : " + "Column value :" + value.getColumn());
//else System.out.print(" ");
System.out.println(" Value : " + value.getValue());
System.out.println();}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}