-4

How to extract a value from an XML, store the value and use it replace file name and folder name using java? I need to read an XML file and extract a value so that it can be used to replace a file and folder name eg;

  1. XML file has a value "Car"
  2. Extract "Car"
  3. Then use that value to replace a filename eg: car.jpg/pdf
  4. Also replace the folder name

Any suggestions the way this can be done? Im using Java, Dom paser and Xpath.

public class ReadAndPrintXMLFile {

    public static void main(String argv[]) {

        String path = "book.xml";

        String output = getName(path);
        String LstName = getLN(path);
        System.out.println("Firstname" + output);
        System.out.println("LastName" + LstName);
        //System.exit (0);

    }//end of main

    public static String getName(String path) {
        try {

            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
            Document doc = docBuilder.parse(new File(path));

            // normalize text representation
            doc
                    .getDocumentElement()
                    .normalize();
            System.out.println("Root element of the doc is " + doc
                    .getDocumentElement()
                    .getNodeName());

            NodeList listOfPersons = doc.getElementsByTagName("person");
            int totalPersons = listOfPersons.getLength();
            System.out.println("Total no of people : " + totalPersons);

            for(int s = 0; s < listOfPersons.getLength(); s++) {

                Node firstPersonNode = listOfPersons.item(s);
                if(firstPersonNode.getNodeType() == Node.ELEMENT_NODE) {

                    Element firstPersonElement = (Element) firstPersonNode;

                    //-------
                    NodeList firstNameList = firstPersonElement.getElementsByTagName("first");
                    Element firstNameElement = (Element) firstNameList.item(0);

                    NodeList textFNList = firstNameElement.getChildNodes();
                    //                  System.out.println("First Name : " +
                    //                                             ((Node)textFNList.item(0)).getNodeValue().trim());


                    return ((Node) textFNList.item(0))
                            .getNodeValue()
                            .trim()
                            .toString();


                }

            }

        } catch(SAXParseException err) {
            System.out.println("** Parsing error" + ", line " + err.getLineNumber() + ", uri " + err.getSystemId());
            System.out.println(" " + err.getMessage());

        } catch(SAXException e) {
            Exception x = e.getException();
            ((x == null) ? e : x).printStackTrace();

        } catch(Throwable t) {
            t.printStackTrace();
        }

        return null;
    }

    public static String getLN(String path) {
        try {

            DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
            Document doc = docBuilder.parse(new File(path));

            // normalize text representation
            doc
                    .getDocumentElement()
                    .normalize();
            System.out.println("Root element of the doc is " + doc
                    .getDocumentElement()
                    .getNodeName());

            NodeList listOfPersons = doc.getElementsByTagName("person");
            int totalPersons = listOfPersons.getLength();
            System.out.println("Total no of people : " + totalPersons);

            for(int s = 0; s < listOfPersons.getLength(); s++) {

                Node lastPersonNode = listOfPersons.item(s);
                if(lastPersonNode.getNodeType() == Node.ELEMENT_NODE) {

                    Element lastPersonElement = (Element) lastPersonNode;

                    //-------
                    NodeList lastNameList = lastPersonElement.getElementsByTagName("last");
                    Element lastNameElement = (Element) lastNameList.item(0);

                    NodeList textLNList = lastNameElement.getChildNodes();
                    //                  System.out.println("First Name : " +
                    //                                             ((Node)textFNList.item(0)).getNodeValue().trim());



                    return ((Node) textLNList.item(0))
                            .getNodeValue()
                            .trim()
                            .toString();

                }

            }

        } catch(SAXParseException err) {
            System.out.println("** Parsing error" + ", line " + err.getLineNumber() + ", uri " + err.getSystemId());
            System.out.println(" " + err.getMessage());

        } catch(SAXException e) {
            Exception x = e.getException();
            ((x == null) ? e : x).printStackTrace();

        } catch(Throwable t) {
            t.printStackTrace();
        }

        return null;
    }


}
lee
  • 23
  • 1
  • 5

1 Answers1

0

You just named the method you have to implement:

public string extractValue(string xmlFilePath, string keyName) {
  //open xml file
  // use Xpath to search for key
  // return value for key
}

public void replaceFilename(string originalFileName, string newFileName) {
  // test for correct paths
  // replace filename
}

public void replaceFoldername(string originalFolderName, string newFolderName) {
  // create new folders
  // move file from old folders to new folders
  // remove old folders (if empty)
}
M. Haverbier
  • 383
  • 2
  • 13
  • This looks like a good outline i'll try something like this and see how I get on. Cheers – lee Jul 18 '17 at 15:47