如何从XML中提取值,存储值并使用它替换使用java替换文件名和文件夹名?

时间:2017-07-18 15:32:28

标签: java xml xpath domparser

如何从XML中提取值,存储值并使用它替换使用java替换文件名和文件夹名?我需要读取一个XML文件并提取一个值,以便它可以用来替换文件和文件夹名称,例如;

  1. XML文件的值为“Car”
  2. 提取“Car”
  3. 然后使用该值替换文件名,例如:car.jpg / pdf
  4. 同时替换文件夹名称
  5. 有什么建议可以这样做吗?我使用Java,Dom paser和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;
        }
    
    
    }
    

1 个答案:

答案 0 :(得分:0)

您刚刚命名了必须实施的方法:

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