如何从XML中提取值,存储值并使用它替换使用java替换文件名和文件夹名?我需要读取一个XML文件并提取一个值,以便它可以用来替换文件和文件夹名称,例如;
有什么建议可以这样做吗?我使用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;
}
}
答案 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)
}