큰 XML 문서의 가운데 요소를 언 마샬하려고합니다. 현재 JAXB와 Woodstox를 사용 중입니다. 나는 비 직렬화 할 필요가 XML 중간 요소의jaxb를 사용하여 xml 문서의 가운데 정렬 해제
예 : 내가 사용하는
<Values>
<Person ID="ABC">
<FirstName>Shawn</FirstName>
<LastName>Mark</LastName>
<Age>3</Age>
</Person>
<Person ID="DEF">
<FirstName>John</FirstName>
<LastName>Durell</LastName>
<Age>4</Age>
</Person>
</Values>
, JAXB 프로 클래스는 다음과 같습니다
@XmlRootElement(name = "Values")
@XmlAccessorType(XmlAccessType.FIELD)
public class Attributes
{
@XmlElement(name = "Person")
private ArrayList<Person> persons;
public ArrayList<Person> getPersons()
{
return persons;
}
}
@XmlAccessorType(XmlAccessType.FIELD)
public class Person
{
@XmlAttribute
private String ID;
@XmlElement(name = "FirstName")
private String firstName;
@XmlElement(name = "LastName")
private String lastName;
@XmlElement(name = "Age")
private String age;
}
나는 ID를 제외한 모든 값을 비 직렬화 할 수 있어요. 그것은 null로 표시됩니다. <Values>
이 루트 5-6 레벨을 중첩 할 때 위의 코드는 작동하고
final XMLInputFactory xif = XMLInputFactory.newInstance();
final StreamSource xml = new StreamSource(pathToxmlFile);
XMLStreamReader xsr;
xsr = xif.createXMLStreamReader(xml);
xsr.nextTag();
while (!xsr.getLocalName().equals("Values"))
{
xsr.nextTag();
}
final JAXBContext jc = JAXBContext.newInstance(Attributes.class);
final Unmarshaller unmarshaller = jc.createUnmarshaller();
final JAXBElement<Attributes> jb = unmarshaller.unmarshal(xsr, Attributes.class);
: 여기
는 코드입니다.<Values>
앞에 15 개의 태그가있는 경우이 코드는 작동하지 않습니다.
또한 JAXB를 사용하고 모든 요소를 비 정렬 화하는 것과 비교할 때 상대적으로 매우 느리지 만 절대 사용되지 않는 데이터 용 개체를 만들어야합니다.
내 질문은 - 어쨌든 성능을 향상시킬 수 있습니까? xml에 중첩 된 경우 왜 작동하지 않습니까? Person 속성에서 ID 값을 가져 오는 방법은 무엇입니까?
예 이렇게하면 성능이 향상되었습니다. 하지만 한 가지 더 질문이 있습니다. JAXB/Stax에 String으로 XML 데이터를 전달하는 파일에서 읽는 대신 오히려 사용할 수있는 다른 클래스/메서드를 사용할 수 있습니까? – Ikshvak
@Ikshvak -'StringReader' 인스턴스에서 XML'String'을 래핑하고 그로부터 비 정렬화할 수 있습니다. JAXB는 비 정렬화할 여러 가지 유형을 지원합니다. –