首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在从MySQL数据库获取数据时在XML文件中附加多个子根节点

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

Stack Overflow用户
提问于 2014-11-15 03:14:40
回答 1查看 1.9K关注 0票数 0

我尝试从MySQL数据库中检索对象并将其存储在XML文件中。然而,下面的代码并没有以我需要的格式写入XML数据。

代码语言:javascript
复制
 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; 
	  } 

下面是im为上述代码获得的输出。

代码语言:javascript
复制
<?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>

但是,我需要以下形式的输出

代码语言:javascript
复制
<?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>

有没有人能给我一些建议,告诉我如何实现上述格式的输出

EN

回答 1

Stack Overflow用户

发布于 2014-11-16 00:39:17

我解决了它

代码语言:javascript
复制
 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;
	  } 

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26937248

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档