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

问题描述:

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

  1. XML文件有一个值 “汽车”
  2. 提取物 “驾驶”
  3. 然后使用该值来代替一个文件名如:car.jpg/PDF
  4. 同时更换文件夹名称

任何建议可以做到这一点?我使用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

你有什么迄今所做? –

+1

对不起,这不是StackOverflow的工作方式。问题形式_“我想做X,请指导我”_被认为是题外话。请访问[帮助]并阅读[问],尤其是阅读[为什么是“有人可以帮助我?”不是一个实际问题?](http://meta.stackoverflow.com/q/284236/18157) –

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

这看起来像一个很好的轮廓,我会尝试这样的事情,看看我如何。干杯 – lee