如何从 XML 中提取值、存储该值并使用它使用 java 替换文件名和文件夹名?
How to extract a value from an XML, store the value and use it replace file name and folder name using java?
如何从 XML 中提取值、存储该值并使用它替换使用 java 的文件名和文件夹名?我需要读取一个 XML 文件并提取一个值,以便它可以用来替换文件和文件夹名称,例如;
- XML 文件有一个值 "Car"
- 摘录"Car"
- 然后使用该值替换文件名,例如:car.jpg/pdf
- 也替换文件夹名称
有什么可以做到这一点的建议吗?我正在使用 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;
}
}
您刚刚命名了要实现的方法:
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)
}
如何从 XML 中提取值、存储该值并使用它替换使用 java 的文件名和文件夹名?我需要读取一个 XML 文件并提取一个值,以便它可以用来替换文件和文件夹名称,例如;
- XML 文件有一个值 "Car"
- 摘录"Car"
- 然后使用该值替换文件名,例如:car.jpg/pdf
- 也替换文件夹名称
有什么可以做到这一点的建议吗?我正在使用 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;
}
}
您刚刚命名了要实现的方法:
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)
}