Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java xml解析器配置文件_Java_Xml_File_Properties_Xml Parsing - Fatal编程技术网

Java xml解析器配置文件

Java xml解析器配置文件,java,xml,file,properties,xml-parsing,Java,Xml,File,Properties,Xml Parsing,我正在用java编写一个简单的XML解析器 请解释如何从属性文件中读取XML标记名? 示例:如果有类似的XML文件 <parent> <child>1</child> <child2>2</child2> </parent> 1. 2. 我想从配置文件中读取标记名。这是我目前正在编写的代码,我正在使用test.xml文件从中读取数据 public class xml2 { public voi

我正在用java编写一个简单的XML解析器

请解释如何从属性文件中读取XML标记名? 示例:如果有类似的XML文件

<parent>
    <child>1</child>
    <child2>2</child2>
</parent>

1.
2.
我想从配置文件中读取标记名。这是我目前正在编写的代码,我正在使用test.xml文件从中读取数据

public class xml2 
{
    public void xmlR(File f)
    {
        try
        {
            FileInputStream IF=new FileInputStream(f);
            DataInputStream data=new DataInputStream(IF);
            BufferedReader buf=new BufferedReader(new InputStreamReader(data));
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(f);
            doc.getDocumentElement().normalize();
            NodeList nList = doc.getElementsByTagName("test");

            File file=new File("xml.txt");
            BufferedWriter writer = new BufferedWriter(new FileWriter(file));

            for (int temp = 0; temp < nList.getLength(); temp++) 
                {

                Node nNode = nList.item(temp);
                if (nNode.getNodeType() == Node.ELEMENT_NODE) 
                {

                    Element eElement = (Element) nNode;
                    writer.write("ID : " + getTag("id", eElement));
                    writer.newLine();
                    writer.write("Name : " + getTag("Name", eElement));
                    writer.newLine();
                    writer.write("AGE: " + getTag("age", eElement));
                    writer.newLine();
                    writer.flush();
                    writer.write("-----------------------");writer.newLine();
                }
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    private static String getTag(String sTag, Element eElement) 
    {
        NodeList nlList = eElement.getElementsByTagName(sTag).item(0).getChildNodes();

        Node nValue = (Node) nlList.item(0);

        return nValue.getNodeValue();
    }

    public static void main(String args[])
    {
        xml2 x=new xml2();
        x.xmlR(new File("test.xml"));
    }
}
公共类xml2
{
公共void xmlR(文件f)
{
尝试
{
FileInputStream IF=newfileinputstream(f);
DataInputStream数据=新的DataInputStream(如果);
BufferedReader buf=新的BufferedReader(新的InputStreamReader(数据));
DocumentBuilderFactory dbFactory=DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder=dbFactory.newDocumentBuilder();
documentdoc=dBuilder.parse(f);
doc.getDocumentElement().normalize();
NodeList nList=doc.getElementsByTagName(“测试”);
File File=新文件(“xml.txt”);
BufferedWriter writer=新的BufferedWriter(新文件编写器(文件));
对于(int-temp=0;temp
您尚未显示配置文件,因此不可能为您提供工作代码,但通常对于这种XML,最简单的方法是使用格式为
/config/param[@name=$xxx]/@value
的XPath表达式从配置文件中获取每个值

您正在使用DOM,这似乎是对XML不熟悉的Java程序员的默认选择。但是DOM是陈旧、臃肿和不友好的:人们使用它的唯一原因是它内置于JDK中。还有更好的选择,比如JDOM和XOM。

我喜欢。我认为这是一个非常优雅的解决方案。