使用java代码读取xml节点

时间:2016-09-24 08:14:27

标签: java xml windows

我连接到许多服务器的数据库以从中检索数据,我使用xml文件包含每个数据库服务器的信息[ip,port,user,passwd],然后在该服务器上执行查询。我在我的代码中读取查询作为arrayList并逐个获取它们进行处理。我需要的是一个清晰简单的代码,也读取数据库服务器信息连接到它并执行相关查询,即我需要在我的代码中获取数据库IP,端口,用户,密码的每个查询。这是我的xml文件结构。谢谢你。

<?xml version="1.0"?>
<Database>
<DB>
<ip></ip>
<port></port>
<user></user>
<pass></pass>
</DB>
<Query>
select date from myTable
</Query>
<DB>
<ip></ip>
<port></port>
<user></user>
<pass></pass>
</DB>
<Query>
select time from myTable
</Query>
<DB>
<ip></ip>
<port></port>
<user></user>
<pass><></pass>
</DB>
<Query>
select name from myTable
</Query>
</Database>

1 个答案:

答案 0 :(得分:0)

您可以使用Java的DOM api进行xml处理和xpath。

下面是一些示例代码,它可以完成您想要的一部分(提取ip和端口)。您将需要修改它来完成剩下的工作:

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;


public class DomTester {


    public static void main(String[] args) throws  ParserConfigurationException, SAXException, IOException, XPathExpressionException {

        InputStream inStr = null;
        try {
            String xml = "<?xml version=\"1.0\"?>"
                    + "<Database>"
                    +   "<DB>"
                    +       "<ip>666</ip>"
                    +       "<port>7</port>"
                    +   "</DB>"
                    +   "<DB>"
                    +   "   <ip>13</ip>"
                    +       "<port>1</port>"
                    +   "</DB>"
                    + "</Database>";

            inStr = new ByteArrayInputStream(xml.getBytes());
            DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
            DocumentBuilder db = dbf.newDocumentBuilder(); 
            Document doc = db.parse(inStr);

            XPathFactory xFactory = XPathFactory.newInstance();
            XPath xpath = xFactory.newXPath();
            XPathExpression expr = xpath.compile("/Database");
            Element databaseEl = (Element)expr.evaluate(doc, XPathConstants.NODE);

            NodeList databaseNodeList = databaseEl.getChildNodes();
            for (int ii=0; ii<databaseNodeList.getLength(); ++ii) {
                Node dbNode = databaseNodeList.item(ii);
                XPathExpression ipExpr = xpath.compile("ip");
                Element ipElement = (Element)ipExpr.evaluate(dbNode, XPathConstants.NODE);
                String ip = ipElement.getTextContent();

                XPathExpression portExpr = xpath.compile("port");
                Element portEl = (Element)portExpr.evaluate(dbNode, XPathConstants.NODE);
                String port = portEl.getTextContent();

                System.out.println("DB node[" + ii + "] = ip: " + ip + " port: " + port);
            }
        } finally {
            if (null != inStr) {
                inStr.close();
            }
        }
    }
}

由于您是从文件中读取而不是使用字符串,而不是这样做:

inStr = new ByteArrayInputStream(xml.getBytes());

这样做:

File f = new File("your/file/path.xml");
inStr = new FileInputStream(f);