如何使用Java从属性xml中获取值?
如何使用Java从属性xml中获取值 我需要使用我列出的java方法获取元素的值,该元素的key=password,然后将Helllo123输出到控制台窗口 属性XML文件:如何使用Java从属性xml中获取值?,java,xml,properties,Java,Xml,Properties,如何使用Java从属性xml中获取值 我需要使用我列出的java方法获取元素的值,该元素的key=password,然后将Helllo123输出到控制台窗口 属性XML文件: <?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"> <properties> <comment>Propert
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Properties Example</comment>
<entry key="myvoucherDummySearchTerm">www.example.com</entry>
<entry key="password">Hello123</entry>
</properties>
public class ChangeToXml {
public static void main(String[] args) throws IOException {
Properties p = new Properties();
FileInputStream fi = new FileInputStream(Base_Page.getConstant(Constant.CONFIG_PROPERTIES_DIRECTORY));
p.load(fi);
FileOutputStream fos = new FileOutputStream("properties.xml");
p.storeToXML(fos, "Properties Example");
testMethod();
}
public static void testMethod() {
try {
File fXmlFile = new File("C://Users//joe.blogs//Desktop//AutoFramework//AutoFramework//properties.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("staff");
System.out.println("----------------------------");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
System.out.println("\nCurrent Element :" + nNode.getNodeName());
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
System.out.println("Staff id : " + eElement.getElementsByTagName("password").item(0).getTextContent());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
属性示例
www.example.com
你好
JAVA方法:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
<comment>Properties Example</comment>
<entry key="myvoucherDummySearchTerm">www.example.com</entry>
<entry key="password">Hello123</entry>
</properties>
public class ChangeToXml {
public static void main(String[] args) throws IOException {
Properties p = new Properties();
FileInputStream fi = new FileInputStream(Base_Page.getConstant(Constant.CONFIG_PROPERTIES_DIRECTORY));
p.load(fi);
FileOutputStream fos = new FileOutputStream("properties.xml");
p.storeToXML(fos, "Properties Example");
testMethod();
}
public static void testMethod() {
try {
File fXmlFile = new File("C://Users//joe.blogs//Desktop//AutoFramework//AutoFramework//properties.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
System.out.println("Root element :" + doc.getDocumentElement().getNodeName());
NodeList nList = doc.getElementsByTagName("staff");
System.out.println("----------------------------");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
System.out.println("\nCurrent Element :" + nNode.getNodeName());
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
System.out.println("Staff id : " + eElement.getElementsByTagName("password").item(0).getTextContent());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
公共类changeTOML{
公共静态void main(字符串[]args)引发IOException{
属性p=新属性();
FileInputStream fi=新的FileInputStream(Base_Page.getConstant(Constant.CONFIG_PROPERTIES_DIRECTORY));
p、 负荷(fi);
FileOutputStream fos=新的FileOutputStream(“properties.xml”);
p、 storeToXML(fos,“属性示例”);
testMethod();
}
公共静态void testMethod(){
试一试{
File fXmlFile=新文件(“C://Users//joe.blogs//Desktop//AutoFramework//AutoFramework//properties.xml”);
DocumentBuilderFactory dbFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder=dbFactory.newDocumentBuilder();
documentdoc=dBuilder.parse(fXmlFile);
doc.getDocumentElement().normalize();
System.out.println(“根元素:+doc.getDocumentElement().getNodeName());
NodeList nList=doc.getElementsByTagName(“staff”);
System.out.println(“-------------------------------”;
对于(int-temp=0;temp
我建议您实现解决方案SAX解析器。SAX解析器是一种基于事件的XML文档解析器。您可以像
try {
File file = new File("test.xml");
FileInputStream fileInput = new FileInputStream(file);
Properties properties = new Properties();
properties.loadFromXML(fileInput);
fileInput.close();
Enumeration enuKeys = properties.keys();
while (enuKeys.hasMoreElements()) {
String key = (String) enuKeys.nextElement();
String value = properties.getProperty(key);
System.out.println(key + ": " + value);
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
您可以使用正则表达式在xml中进行解析 这对你有帮助吗
public static void main(String[] args) throws IOException {
String content = new String(Files.readAllBytes(Paths.get("properties.xml")));
System.out.println(Arrays.toString(getTagValues(content).toArray())); // Prints [Hello123]
}
private static final Pattern TAG_REGEX = Pattern.compile("<entry key=\"password\">(.+?)</entry>");
private static List<String> getTagValues(final String str) {
final List<String> tagValues = new ArrayList<String>();
final Matcher matcher = TAG_REGEX.matcher(str);
while (matcher.find()) {
tagValues.add(matcher.group(1));
}
return tagValues;
}
publicstaticvoidmain(字符串[]args)引发IOException{
字符串内容=新字符串(Files.readAllBytes(path.get(“properties.xml”));
System.out.println(Arrays.toString(getTagValues(content.toArray());//Prints[Hello123]
}
私有静态最终模式标记_REGEX=Pattern.compile((.+?));
私有静态列表getTagValues(最终字符串str){
最终列表tagValues=new ArrayList();
最终匹配器匹配器=TAG_REGEX.Matcher(str);
while(matcher.find()){
tagValues.add(matcher.group(1));
}
返回标记值;
}
这与您今天提出的其他问题有关吗?如果是的话,我想你是在想你不应该去的地方
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Properties;
public class ChangeToXml {
public static void main(String[] args) throws FileNotFoundException, IOException {
Properties p = new Properties();
//load from your original file;
FileInputStream inputProps = new FileInputStream("C:\\tmp\\config.properties");
p.load(inputProps);
//store in xml format
FileOutputStream outputXml = new FileOutputStream("C:\\tmp\\properties.xml");
p.storeToXML(outputXml, "Properties Example");
//load from xml
FileInputStream inputXml = new FileInputStream("C:\\tmp\\properties.xml");
p.loadFromXML(inputXml);
// get key value pair in the same way as from your original file
String browser = p.getProperty("browser");
String url = p.getProperty("url");
System.out.println("browser: " + browser);
System.out.println("url: " + url);
}
}