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.

9
  • Yes, Sql DBMSes have better way to do it. Those tools are DBMS specific. Commented Dec 13, 2019 at 10:37
  • could you elaborate? Commented Dec 13, 2019 at 10:39
  • could you elaborate? In order to elaborate, you need to tell us which DBMS you are using. Commented Dec 13, 2019 at 11:21
  • I am using Oracle Commented Dec 13, 2019 at 11:32
  • 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 stackoverflow.com/questions/14264953/… Commented Dec 13, 2019 at 11:46

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.