0

I need to update specific items in a database. These items can be found from the database by using the ID-value found from the XML-file.

The XML-file is something like this:

<Employees>
    <Employee>
        <id>123</id>
        <age>29</age>
        <role>Java Developer</role>
        <gender>Male</gender>
    </Employee>
    <Employee>
        <id>234</id>
        <age>35</age>
        <role>CSS Developer</role>
        <gender>Female</gender>
    </Employee>
</Employees>

Is it possible to gather the IDs to an array, then loop the array through an sql update method? Is looping an sql query method bad practise or is there another way?

Also can this be done without XML parsers such as DOM, SAX or JAXB to keep it simple?

EDIT1

This is my solution for extracting ID values from XML without using XML parsers:

    public static void main(String[] args) throws Exception {

        try {
            File fXmlFile = new File("employee.xml");
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder;
            dBuilder = dbFactory.newDocumentBuilder();
            Document doc = (Document) dBuilder.parse(fXmlFile);
            doc.getDocumentElement().normalize();

            NodeList employeeIdNodeList=doc.getElementsByTagName("id");
            for(int k=0;k<employeeIdNodeList.getLength();k++) {
                Node employeeIdNode = (Node) employeeIdNodeList.item(k);
                System.out.println(employeeIdNode.getTextContent());
            }
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

Any thoughts? Is it efficient? It is based on XML Parse for specific element in java. There will be max 10 employees in the XML.

willberthos
  • 305
  • 1
  • 4
  • 15
  • Yes, Sql DBMSes have better way to do it. Those tools are DBMS specific. – Serg Dec 13 '19 at 10:37
  • could you elaborate? – willberthos Dec 13 '19 at 10:39
  • _could you elaborate?_ In order to elaborate, you need to tell us which DBMS you are using. – Abra Dec 13 '19 at 11:21
  • I am using Oracle – willberthos Dec 13 '19 at 11:32
  • _"Also can this be done without XML parsers such as DOM, SAX or JAXB to keep it simple?"_ - If you have XML you should use a decent XML parser. Don't even think about using something like regex to extract the IDs. – vanje Dec 13 '19 at 11:32
  • @vanje I was thinking about looping the elements like this: [https://stackoverflow.com/questions/48710928/xml-parse-for-specific-element-in-java](https://stackoverflow.com/questions/48710928/xml-parse-for-specific-element-in-java) – willberthos Dec 13 '19 at 11:34
  • 1
    If your XML file is relatively small, DOM + XPath would be the simplest solution. And you should consider to group your UPDATE calls with JDBC batch calls. See https://stackoverflow.com/questions/14264953/how-is-jdbc-batch-update-helpful – vanje Dec 13 '19 at 11:46
  • Database table EMPLOYEE has primary key column ID and other columns including AGE, ROLE and GENDER. You need to read a XML file containing a list of employees and update the EMPLOYEE table with the data in the XML file. Is that a correct description of the task you need to perform? – Abra Dec 13 '19 at 11:58
  • yes, that is the correct description. – willberthos Dec 13 '19 at 12:06

0 Answers0