2014-11-14 3 views
0

Я пытаюсь извлечь объекты из базы данных MySQL и сохранить их в XML-файле. Как бы то ни было, приведенный ниже код не записывает данные XML в нужном мне формате.Как добавить несколько дочерних корневых узлов в XML-файл при извлечении данных из базы данных MySQL

private static Document buildCustomerXML(ResultSet EmployeeRS) throws Exception 
 
    { 
 

 
    Document xmlDoc = new DocumentImpl(); 
 
    
 
    /* Creating the root element */ 
 
    
 
    Element rootElement = xmlDoc.createElement("Schedule"); 
 
    xmlDoc.appendChild(rootElement); 
 
    while(EmployeeRS.next()) 
 
\t { 
 
\t Element employee1 = xmlDoc.createElement("Employees"); 
 
\t \t Element employee = xmlDoc.createElement("Employee"); 
 

 
\t \t  /* Build the CustomerId as a Attribute*/ 
 
\t \t  employee.setAttribute("id", EmployeeRS.getString("id")); 
 

 
\t \t  /* Creating elements within customer DOM*/ 
 
\t \t  Element contractid = xmlDoc.createElement("ContractID"); 
 
\t \t  Element lastName = xmlDoc.createElement("Name"); 
 
\t \t  Element skills = xmlDoc.createElement("Skills"); 
 
\t \t  Element skill = xmlDoc.createElement("Skill"); 
 
\t \t  /* Populating Customer DOM with Data*/ 
 
\t \t  contractid.appendChild(xmlDoc.createTextNode(EmployeeRS.getString("Contractid"))); 
 
\t \t  lastName.appendChild(xmlDoc.createTextNode(EmployeeRS.getString("last"))); 
 
\t \t  
 
\t \t  skill.appendChild(xmlDoc.createTextNode(EmployeeRS.getString("Skill"))); 
 
\t \t  
 
\t \t  /* Adding the firstname and lastname elements to the Customer Element*/ 
 
\t \t  employee.appendChild(contractid); 
 
\t \t  employee.appendChild(lastName); 
 
\t \t employee.appendChild(skills); 
 
\t \t  skills.appendChild(skill); 
 
\t \t  employee1.appendChild(employee); 
 
\t \t  rootElement.appendChild(employee1); 
 
\t \t  
 
\t \t 
 
\t \t  
 

 
\t \t  /* Appending Customer to the Root Class*/ 
 
\t \t  
 
\t \t  
 
\t \t  /* Build the CustomerId as a Attribute*/ 
 
\t \t  Element constraints1 = xmlDoc.createElement("Constraints"); 
 
\t \t  Element constraints = xmlDoc.createElement("Constraint"); 
 
\t \t  constraints.setAttribute("id", EmployeeRS.getString("id")); 
 
\t \t  Element startdat = xmlDoc.createElement("startdate"); 
 
\t \t  startdat.appendChild(xmlDoc.createTextNode(EmployeeRS.getString("startdate"))); 
 
\t \t  constraints.appendChild(startdat); 
 
\t \t  constraints1.appendChild(constraints); 
 
\t \t  
 
\t \t  rootElement.appendChild(constraints1); 
 
\t \t  
 
\t } 
 
\t return xmlDoc; 
 
\t }

Ниже выход им получить для приведенного выше кода

<?xml version="1.0" encoding="UTF-8"?> 
 
<Schedule> 
 
    <Employees> 
 
     <Employee id="11"> 
 
      <ContractID>1</ContractID> 
 
      <Name>kumbhar</Name> 
 
      <Skills> 
 
       <Skill>Employee</Skill> 
 
      </Skills> 
 
     </Employee> 
 
    </Employees> 
 
    <Constraints> 
 
     <Constraint id="11"> 
 
      <startdate>11/08/2014</startdate> 
 
     </Constraint> 
 
    </Constraints> 
 
    <Employees> 
 
     <Employee id="14"> 
 
      <ContractID>1</ContractID> 
 
      <Name>Raje</Name> 
 
      <Skills> 
 
       <Skill>Employee</Skill> 
 
      </Skills> 
 
     </Employee> 
 
    </Employees> 
 
    <Constraints> 
 
     <Constraint id="14"> 
 
      <startdate>2014-11-12</startdate> 
 
     </Constraint> 
 
    </Constraints> 
 
</Schedule>

Однако мне нужно вывод, что в приведенной ниже форме

<?xml version="1.0" encoding="UTF-8"?> 
 
<Schedule> 
 

 
    <Employees> 
 
     <Employee id="11"> 
 
      <ContractID>1</ContractID> 
 
      <Name>kumbhar</Name> 
 
      <Skills> 
 
       <Skill>Employee</Skill> 
 
      </Skills> 
 
     </Employee> 
 
     <Employee id="14"> 
 
      <ContractID>1</ContractID> 
 
      <Name>Raje</Name> 
 
      <Skills> 
 
       <Skill>Employee</Skill> 
 
      </Skills> 
 
     </Employee> 
 
    </Employees> 
 
    
 
    
 
    <Constraints> 
 
     <Constraint id="14"> 
 
      <startdate>2014-11-12</startdate> 
 
     </Constraint>   
 
     <Constraint id="11"> 
 
      <startdate>11/08/2014</startdate> 
 
     </Constraint> 
 
    
 
    </Constraints> 
 
</Schedule>

Может ли один предоставить мне предложения о том, как добиться выхода в вышеуказанном формате

ответ

0

Я решил его

while(EmployeeRS.next()) 
 
\t { 
 
\t  
 
\t \t Element employee = xmlDoc.createElement("Employee"); 
 

 
\t \t  /* Build the CustomerId as a Attribute*/ 
 
\t \t  employee.setAttribute("id", EmployeeRS.getString("id")); 
 

 
\t \t  /* Creating elements within customer DOM*/ 
 
\t \t  Element contractid = xmlDoc.createElement("ContractID"); 
 
\t \t  Element lastName = xmlDoc.createElement("Name"); 
 
\t \t  Element skills = xmlDoc.createElement("Skills"); 
 
\t \t  Element skill = xmlDoc.createElement("Skill"); 
 
\t \t  /* Populating Customer DOM with Data*/ 
 
\t \t  contractid.appendChild(xmlDoc.createTextNode(EmployeeRS.getString("Contractid"))); 
 
\t \t  lastName.appendChild(xmlDoc.createTextNode(EmployeeRS.getString("last"))); 
 
\t \t  
 
\t \t  skill.appendChild(xmlDoc.createTextNode(EmployeeRS.getString("Skill"))); 
 
\t \t  
 
\t \t  /* Adding the firstname and lastname elements to the Customer Element*/ 
 
\t \t  employee.appendChild(contractid); 
 
\t \t  employee.appendChild(lastName); 
 
\t \t employee.appendChild(skills); 
 
\t \t  skills.appendChild(skill); 
 
\t \t  employee1.appendChild(employee); 
 
\t \t  
 
\t \t  \t \t  /* Appending Customer to the Root Class*/ 
 
\t \t  
 
\t \t  
 
\t \t  /* Build the CustomerId as a Attribute*/ 
 
\t \t  
 
\t \t  Element constraints = xmlDoc.createElement("Constraint"); 
 
\t \t  constraints.setAttribute("id", EmployeeRS.getString("id")); 
 
\t \t  Element startdat = xmlDoc.createElement("startdate"); 
 
\t \t  Element enddat = xmlDoc.createElement("enddate"); 
 
\t \t  startdat.appendChild(xmlDoc.createTextNode(EmployeeRS.getString("startdate"))); 
 
\t \t  enddat.appendChild(xmlDoc.createTextNode(EmployeeRS.getString("enddate"))); 
 
\t \t  constraints.appendChild(startdat); 
 
\t \t  constraints.appendChild(enddat); 
 
\t \t  constraints1.appendChild(constraints); 
 
\t \t  
 
\t \t  
 
\t \t  
 
\t \t  
 
\t \t  
 
\t } rootElement.appendChild(employee1); 
 
\t rootElement.appendChild(constraints1); 
 
\t return xmlDoc; 
 
\t }