2012-10-17 2 views
1

동일한 XML 요소로 다른 ID를 사용하여 jaxb 마샬링을 루프하기 만하면 아래 코드와 달리 은 각 ID에 대한 새 XML 파일을 구문 분석하고 이전 하나를 쓰십시오..루프 a jaxb marshall

data [] []는 ID 목록을 나타내는 data [i] [0]에서 읽습니다.이 ID를 고객 ID로 설정하고 싶습니다. 위의 코드에서

 Customer customer = new Customer(); 
     File file = new File("C:/data.xml"); 

     for (int i = 0; i < data.length; i++) { 
      customer.setId(data[i][0]); 

      JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class); 
      Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); 


      jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 

      jaxbMarshaller.marshal(customer, file); 
      jaxbMarshaller.marshal(customer, System.out); 


     } 

출력 : 나는 하나 개의 XML 파일의 모든 ID 출력을 원하는

<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<customer id="1"/> 
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<customer id="2"/> 
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<customer id="3"/> 
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<customer id="4"/> 
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> 
<customer id="5"/> 

, 모든 팁?

답변

2

Marshaller에 머리글이 쓰여지지 않도록 JAXB_FRAGMENT 속성을 설정할 수 있습니다. 다른 문서로 정렬 할 때 필요합니다.

package forum12925616; 

import java.io.*; 
import javax.xml.bind.*; 

public class Demo { 

    private static int X = 5; 
    private static int Y = 2; 

    public static void main(String[] args) throws Exception { 
     int[][] data = new int[X][Y]; 
     for (int x = 0; x < X; x++) { 
      for (int y = 0; y < Y; y++) { 
       data[x][y] = x; 
      } 
     } 

     // Create this once 
     JAXBContext jaxbContext = JAXBContext.newInstance(Customer.class); 
     Marshaller jaxbMarshaller = jaxbContext.createMarshaller(); 
     jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); 
     jaxbMarshaller.setProperty(Marshaller.JAXB_FRAGMENT, true); 

     marshal(jaxbMarshaller, data, System.out); 

     FileOutputStream fileOutputStream = new FileOutputStream("src/forum12925616/out.xml"); 
     marshal(jaxbMarshaller, data, fileOutputStream); 
     fileOutputStream.close(); 
    } 

    private static void marshal(Marshaller jaxbMarshaller, int[][] data, OutputStream outputStream) throws Exception { 
     OutputStreamWriter writer = new OutputStreamWriter(outputStream); 
     writer.write("<?xml version='1.0' encoding='UTF-8' standalone='yes'?>"); 
     writer.write("\n<customers>\n"); 
     for (int i = 0; i < data.length; i++) { 
      Customer customer = new Customer(); 
      customer.setId(data[i][0]); 
      jaxbMarshaller.marshal(customer, writer); 
      writer.write("\n"); 
     } 
     writer.write("<customers>"); 
     writer.flush(); 
    } 

} 

시스템 및 파일 출력 아래

은 콘솔과 파일 모두에 결과가 출력됩니다. 추가 정보

<?xml version='1.0' encoding='UTF-8' standalone='yes'?> 
<customers> 
<customer id="0"/> 
<customer id="1"/> 
<customer id="2"/> 
<customer id="3"/> 
<customer id="4"/> 
<customers> 

+0

나는 내 문제를 이해하지 못했다. 예를 들어 나는 10 개의 id를 갖고 있고 루프 안에 마샬링한다. 매번 새로운 id가 생길 때마다, 이전 id를 덮어 쓴다. 그래서 마지막으로 내 xml 파일은 마지막 id 만 보여준다. 그래서 새로운 xml 파일이 생성 될 때마다, 콘솔은 파일의 내용을 보여줍니다. 왜 그렇게 많은 헤더가 있는지입니다. 하나의 단일 XML 파일에 모든 id 출력을 표시하므로 헤더가 콘솔에도 한 번 표시됩니다. – nihulus

+0

@nihulus - 내 대답을 업데이트했습니다. 그것이 당신이 찾고있는 것이라면 저에게 알려주십시오. 덕분에 –

+1

! 이상을 얻었다! – nihulus

1

marhsaller의 구성을 루프 밖으로 이동하십시오. 동일한 스레드에서 원하는만큼 마샬 러를 재사용 할 수 있습니다.

아니면 정말 고객 목록을 단일 XML 파일로 출력하려고합니까? 그런 다음 고객 객체의 배열이 필요하고 배열을 마샬링합니다.

+0

그래, 내가 출력에 하나의 XML 파일로 고객의 목록을합니다. 그래서 Customer 객체의 배열을 생성합니다. Customer [] customer = new Customer [];'어떻게 값을 data [] []로 설정합니까? – nihulus

+0

int i = 0; for (Customer cust : 고객) cust.setId (data [i ++] [0]); – Zagrev

+0

오, 각 고객을 독립적으로 만들어야합니다. 당신은 고객 [i] = 새로운 고객()을 알고 있습니다; – Zagrev