【问题标题】:How to append multiple child root nodes in an XML file while fetching data from MySQL Database从 MySQL 数据库获取数据时如何在 XML 文件中附加多个子根节点
【发布时间】:2014-11-14 19:14:39
【问题描述】:

我正在尝试从 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>

谁能给我一些关于如何实现上述格式输出的建议

【问题讨论】:

    标签: mysql xml dom xml-serialization xstream


    【解决方案1】:

    我解决了

     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;
    	  } 
     

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-07-06
      • 1970-01-01
      • 2019-05-24
      相关资源
      最近更新 更多