Java中将XML数据转换为DOM对象的CLOB字段
//employees.xmlJava中将XML数据转换为DOM对象的CLOB字段,java,oracle,dom,xml-parsing,clob,Java,Oracle,Dom,Xml Parsing,Clob,//employees.xml <employees> <employee id="111"> <firstName>Lokesh</firstName> <lastName>Gupta</lastName> <location>India</location> </employee> <employee i
<employees>
<employee id="111">
<firstName>Lokesh</firstName>
<lastName>Gupta</lastName>
<location>India</location>
</employee>
<employee id="222">
<firstName>Alex</firstName>
<lastName>Gussin</lastName>
<location>Russia</location>
</employee>
<employee id="333">
<firstName>David</firstName>
<lastName>Feezor</lastName>
<location>USA</location>
</employee>
</employees>
public class Employee
{
private Integer id;
private String firstName;
private String lastName;
private String location;
//Setters and Getters
@Override
public String toString()
{
return "Employee [id=" + id + ", firstName=" + firstName + ", lastName=" + lastName + ", location=" + location + "]";
}
}
public static void main(String args[]) throws ParserConfigurationException, SAXException, IOException
{
List<Employee> employees = parseXML();
System.out.println(employees);
}
private static List<Employee> parseXML() throws ParserConfigurationException, SAXException, IOException
{
//Initialize a list of employees
List<Employee> employees = new ArrayList<Employee>();
Employee employee = null;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse(new File("employees.xml"));
document.getDocumentElement().normalize();
NodeList nList = document.getElementsByTagName("employee");
for (int temp = 0; temp < nList.getLength(); temp++)
{
Node node = nList.item(temp);
if (node.getNodeType() == Node.ELEMENT_NODE)
{
Element eElement = (Element) node;
//Create new Employee Object
employee = new Employee();
employee.setId(Integer.parseInt(eElement.getAttribute("id")));
employee.setFirstName(eElement.getElementsByTagName("firstName").item(0).getTextContent());
employee.setLastName(eElement.getElementsByTagName("lastName").item(0).getTextContent());
employee.setLocation(eElement.getElementsByTagName("location").item(0).getTextContent());
//Add Employee to list
employees.add(employee);
}
}
return employees;
}
output : [Employee [id=111, firstName=Lokesh, lastName=Gupta, location=India],
Employee [id=222, firstName=Alex, lastName=Gussin, location=Russia],
Employee [id=333, firstName=David, lastName=Feezor, location=USA]]
Question :
洛克斯
古普塔
印度
亚历克斯
古斯
俄罗斯
大卫
菲佐
美国
公营雇员
{
私有整数id;
私有字符串名;
私有字符串lastName;
私有字符串位置;
//二传手和接球手
@凌驾
公共字符串toString()
{
返回“Employee[id=“+id+”,firstName=“+firstName+”,lastName=“+lastName+”,location=“+location+”””;
}
}
公共静态void main(字符串args[])抛出ParserConfiguration异常、SAXException、IOException
{
List employees=parseXML();
系统输出打印LN(员工);
}
私有静态列表parseXML()抛出ParserConfiguration异常、SAXException、IOException
{
//初始化员工列表
List employees=new ArrayList();
Employee=null;
DocumentBuilderFactory工厂=DocumentBuilderFactory.newInstance();
DocumentBuilder=factory.newDocumentBuilder();
documentdocument=builder.parse(新文件(“employees.xml”);
document.getDocumentElement().normalize();
NodeList nList=document.getElementsByTagName(“员工”);
对于(int-temp=0;temp
//documentdocument=builder.parse(新文件(“employees.xml”)
现在,我在表'A的clob字段“form_data”中有xml数据,而不是employees.xml,我希望builder.parse(新文件(“employees.xml”)“替换它并获取文档对象
我已将表A中的数据放入结果集中:从A中选择表单_数据;
在结果集中,我将表单_数据作为clob,resultSet.getClob(1);我被困在这里,无法从这个clob中获取要解析的文档对象到目前为止您尝试了什么?使用您首选的搜索引擎查找一些教程。首先了解如何从数据库中读取clob字段。然后了解如何用Java解析给定的XML。将这两个对象放在一起,您就完成了。如果遇到问题,请尝试创建一个。在这里提出下一个问题之前,您还应该访问和。