내가이 개 다음과 같은 클래스가이 클래스의계층 구조가있는 경우 JAXB.newInstance를 처리하는 방법은 무엇입니까?
public class Solver implements FunctionI {
List<Double> pointsOfIntersectionX = new ArrayList<>();
List<Double> pointsOfIntersectionY = new ArrayList<>();
..realization
및
public class PolinomialFunction implements FunctionI{
private List<Double> arrayX;
private List<Double> arrayY;
..realization
실현 형태를 내가 하나를 만들 수없는 서로 다릅니다. 내가
public interface XMLinput {
static void writeToXMLForFunction(PolinomialFunction pol, String fileName) throws JAXBException {
JAXBContext jaxbContext = JAXBContext.newInstance(PolinomialFunction.class);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(pol, new File(fileName));
System.out.println("XML");
}
static void writeToXMLForResult(Solver solver, String fileName) throws JAXBException {
JAXBContext jaxbContext = JAXBContext.newInstance(Solver.class);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(solver, new File(fileName));
System.out.println("XML");
}
이 내 문제를 처리하는 방법을 방법이지만, 궁금 다음 한 다음
, 나는 XML 파일로 내 수업 (찾기/PolinomialFunction을) 글을 하나 개의 기능을 할 수 있습니까? 다음을 수행 같은 내 모든 시도 :
JAXBContext jaxbContext = JAXBContext.newInstance(FunctionI.class);
또는 대신 인터페이스의 추상 클래스에 FunctionI를 변경하고이 라인에서 에러 같은 리드를하고. 어떻게 처리 할 수 있습니까?
는
솔루션이 해결 :
static void writeToXML(FunctionI pol, String fileName, Class Tclass) throws JAXBException {
JAXBContext jaxbContext = JAXBContext.newInstance(Tclass);
Marshaller marshaller = jaxbContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(pol, new File(fileName));
System.out.println("XML");
}
및 메서드 호출
XMLinput.writeToXML(polinomialFunctionG, file.getCanonicalPath(), polinomialFunctionG.getClass());
답장을 보내 주셔서 감사합니다. 귀하의 질문에 따르면 : 내가 아는 한 마샬링과 주석을 사용하지 않고 언 마샬링 할 수 있습니다 (예를 들어 설명했듯이 주석이없고 잘 작동하고 있습니다). 또한 귀하의 솔루션에 대한 질문이 있습니다. 향후 PolinomialFunction뿐만 아니라 다른 기능 (예 : LinearFunction)을 사용해야합니다. newInstance (LinearFunction.class 추가)를 수정해야합니까? 하지만 어떤 이유로이 문제를 수정할 수 없다면 어떻게 될까요? 내 질문 섹션에서 내 솔루션에 대해 어떻게 생각하십니까? – RaiseLtwiCe
1. 예 (예 : 대답)에서 배열의 클래스를 사용한다면'newInstance'를 수정해야합니다. 클래스를 하나의 패키지에 넣은 다음 동일한 패키지에 새 클래스를 추가하는 것을 고려해 볼 수 있습니다 ... 클래스에 넣을 외부 속성을 만들거나 다른 방법으로 할 수 있습니다.2 귀하의 솔루션에 대해 - 그것은 객체에 대한 큰 세트가 아니라 한 번만 프로그램을 실행하는 것이고 병렬 요청 처리 (예 : 앱 서버의 웹 서비스)에 관한 것이 아니라면 좋습니다. 그렇지 않으면 성능이 큰 관심사입니다. 새로운 JAXBContext는 특히 등록 할 클래스가 많은 경우 매우 비싼 작업입니다. – Vadim
JAXBContext를 한 번 생성하고 다시 사용하는 것이 일반적입니다. 사례에 대한 예로서 클래스 당 컨텍스트지도를 만들 수 있습니다. 그런 다음 해당 클래스의 첫 번째 호출에 대해서만 새 컨텍스트를 만든 다음지도에 넣습니다. 다음 번에는 준비된 상황을지도에서 꺼내게됩니다. 여전히 클래스의 속성이'List'처럼 간단하지는 않지만 중첩 된 객체 인 경우 - JAXBContext는 클래스를 등록해야합니다. –
Vadim