当前位置: 动力学知识库 > 问答 > 编程问答 >

read xml nodes using java code

问题描述:

i am connecting to database of many servers to retrieve data from them all, i am using xml file contains the information of each database server [ip,port,user,passwd] followed by the query to be executed on that server. i read the queries in my code as arrayList and get them one by one to be processed. what i need is a clear simple code to also read the database server info to connect to it and execute the relative query , i.e. for each a query i need to get the database IP,port,user,passwd in my code. here is my xml file structure. thanks in adavnce.

<?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>

网友答案:

You can use Java's DOM api for xml processing and xpath.

  • DOM: https://docs.oracle.com/javase/tutorial/jaxp/dom/index.html
  • Xpath: http://www.w3schools.com/xsl/xpath_syntax.asp

Below is some sample code that does part of what you want (extracts the ip and port). You will need to modify it to do the rest:

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();
            }
        }
    }
}

Since you are reading from a file instead of using a string, instead of doing this:

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

do this:

File f = new File("your/file/path.xml");
inStr = new FileInputStream(f);
分享给朋友:
您可能感兴趣的文章:
随机阅读: