如何从MySQL数据库中获取数据时在XML文件中附加多个子根节点

时间:2014-11-14 19:14:40

标签: mysql xml dom xml-serialization xstream

我试图从MySQL数据库中检索对象并将其存储在XML文件中。以下代码如何不以我需要的格式编写XML数据。

 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()) 
	   { 
	   Element employee1 = xmlDoc.createElement("Employees");
		  Element employee = xmlDoc.createElement("Employee"); 

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

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

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

以下是上述代码的输出

<?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>

任何人都可以提供有关如何以上述格式实现输出的建议

1 个答案:

答案 0 :(得分:0)

我解决了它

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

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

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