Comment analyser et créer des pojo Java pour les fichiers XML ci-dessous de manière efficace? Veuillez suggérer un analyseur syntaxique efficace.
Le format XML est
<?xml version="1.0" encoding="utf-8"?>
<CCMainRootTag ID="12">
<Header TableName="TableName" TableVersion="12" TableID="12" CreatedDate="2013-02-09T15:35:33" CreatedByUserName="ABC" CreatedBySystem="ABC" />
<ClassPrimary ID="12" Code="Y" DescriptionDK="DK language " DescriptionUK="" DefDK="" DefUK="" IFDGUID="">
<ObjectClass ID="12" Code="YA" DescriptionDK="DK Language" DescriptionUK="" DefDK="" DefUK="" IFDGUID="">
<Synonym>
<Concept Description="Description" Language="DK" />
<Concept Description="" Language="UK" />
<Concept Description="Description" Language="DK" />
<Concept Description="" Language="UK" />
<Concept Description="Description" Language="DK" />
<Concept Description="" Language="UK" />
<Concept Description="Description" Language="DK" />
<Concept Description="" Language="UK" />
</Synonym>
</ObjectClass>
<ObjectClass ID="12" Code="YB" DescriptionDK="DK Language" DescriptionUK="" DefDK="" DefUK="" IFDGUID=""> </ObjectClass>
<ObjectClass ID="12" Code="YC" DescriptionDK="DK Language" DescriptionUK="" DefDK="" DefUK="" IFDGUID=""> </ObjectClass>
<ObjectClass ID="12" Code="YD" DescriptionDK="DK language" DescriptionUK="" DefDK="" DefUK="" IFDGUID=""> </ObjectClass>
</ClassPrimary>
</CCMainRootTag>
J'utilise déjà ce Link mais ses performances sont lentes et le problème n'est pas valide.
Je veux un analyseur syntaxique qui me fournit directement un pojo Java de manière efficace.
Vous pouvez utiliser JAXB pour convertir du XML en POJO Java. Mais avant de finaliser la solution consultez ce site pour une comparaison des performances.
Pour ceux qui recherchent du code JAXB pour convertir xml en objet Java:
//Convert xml to String first
Element partyLoaderRequest; // your xml data
String xmlString = new XMLOutputter().outputString(partyLoaderRequest);
InputStream is = new ByteArrayInputStream(xmlString.getBytes());
DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document document = docBuilder.parse(is);
org.w3c.dom.Element varElement = document.getDocumentElement();
JAXBContext context = JAXBContext.newInstance(Person.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
JAXBElement<Person> loader = unmarshaller.unmarshal(varElement, Person.class);
Person inputFromXml = loader.getValue();
alors que Person a les annotations XML appropriées:
@XmlRootElement(name="Person")
public class CimbWlAdminUserAmendInput {
@XmlElement(name="companyName",required=true,nillable=false)
private String companyName;
...
//setters getters
@XmlTransient
public String getCompanyName() {
return companyName;
}
public void setCompanyName(String companyName) {
this.companyName = companyName;
}
}
La sérialisation et la désérialisation peuvent être gérées par JacksonXmlModule.
// Item.class - use lombok or create g/setters
@JsonPropertyOrder({"name", "description", "note"})
public class Item {
private String name;
private String description;
private String note;
}
// Test.class
package hello.service;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.dataformat.xml.JacksonXmlModule;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import com.fasterxml.jackson.dataformat.xml.annotation.JacksonXmlRootElement;
import hello.entity.Item;
import org.junit.Before;
import org.junit.Test;
import Java.util.ArrayList;
import Java.util.List;
import static org.junit.Assert.*;
public class NameServiceImplTest {
ObjectMapper objectMapper;
@Before
public void setup() {
JacksonXmlModule xmlModule = new JacksonXmlModule();
xmlModule.setDefaultUseWrapper(false);
this.objectMapper = new XmlMapper(xmlModule);
this.objectMapper.enable(SerializationFeature.INDENT_OUTPUT);
}
@Test
public void serializeTest() {
// Wrapper
@JacksonXmlRootElement(localName = "names")
class Names {
public List<Item> item = new ArrayList<>();
}
Item item = new Item();
item.setName("Vladimir");
item.setDescription("Desc");
item.setNote("Note");
Item item2 = new Item();
item2.setName("Iva");
item2.setDescription("Desc2");
item2.setNote("Note2");
Names names = new Names();
names.item.add(item);
names.item.add(item2);
try {
String xml = objectMapper.writeValueAsString(names);
assertNotNull(xml);
System.out.println(xml);
} catch (Exception e) { // IOException
System.out.println(e.getMessage());
fail();
}
}
@Test
public void deserializeTest() {
String xml = "<names>" +
"<item><name>name</name><description>desc</description><note>note</note></item>" +
"<item><name>name</name><description>desc</description><note>note</note></item>" +
"</names>";
try {
List<Item> names = objectMapper.readValue(xml, new TypeReference<List<Item>>() {});
names.forEach(item -> {
assertEquals("name", item.getName());
assertEquals("desc", item.getDescription());
assertEquals("note", item.getNote());
} );
} catch (Exception e) { // IOException
}
}
}