如何从Java中使用异构标记从自定义XML中获取值?

时间:2016-06-23 23:41:02

标签: java xml parsing jaxb

我有一个像

这样的自定义XML
<InputData>
    <dbproperties>
        <dburl>dbURL</dburl>
        <dbuser>DBUSER</dbuser>
        <dbpasskey>DBPASS</dbpasskey>
        <driver>oracle.jdbc.driver.OracleDriver</driver>    
    </dbproperties>
    <tdata>
        <size>4</tdata>    
        <tcount>6</tcount>
    </threaddata>
    <qdata>
        <sql>
            select syadate from dual;
        </sql>
    </qdata>
</InputData>

现在我需要阅读此XML并需要获取每个标记的值。喜欢

dburl = dbURL
dbuser = DBUSER
dbpasskey = DBPASS
driver = oracle.jdbc.driver.OracleDriver
size = 4
tcount = 6
sql = select syadate from dual;

如何以高效的方式执行此操作,而无需在Java类中对任何内容进行硬编码。

即,我输入xml文件名。 Java类应该打印上面的数据输出。

3 个答案:

答案 0 :(得分:0)

步骤1:将XML保存到系统上的文件(例如&#34; d:/test.xml")。

<InputData>
    <dbproperties>
        <dburl>dbURL</dburl>
        <dbuser>DBUSER</dbuser>
        <dbpasskey>DBPASS</dbpasskey>
        <driver>oracle.jdbc.driver.OracleDriver</driver>    
    </dbproperties>
    <tdata>
        <size>4</size>    
        <tcount>6</tcount>
    </tdata>
    <qdata>
        <sql>
            select syadate from dual;
        </sql>
    </qdata>
</InputData>

步骤2:现在,创建以下类来运行您的程序。

InputDataDemo.java

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;

import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

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

public class InputDataDemo {
    public static final String DB_URL = "dburl";
    public static final String DB_USER = "dbuser";
    public static final String DB_PASS_KEY = "dbpasskey";
    public static final String DRIVER = "driver";
    public static final String SIZE = "size";
    public static final String TCOUNT = "tcount";
    public static final String SQL = "sql";

    public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
        File inputFile = new File("D:/test.xml");
        Map<String, String> map = getInputDataMap(new FileInputStream(inputFile));
        Set<String> keySet = map.keySet();
        for (String key : keySet) {
            System.out.println(key + ": " + map.get(key).trim());
        }
    }

    public static Map<String, String> getInputDataMap(InputStream inputStream)
            throws ParserConfigurationException, SAXException, IOException {

        Map<String, String> inputMap = new LinkedHashMap<String, String>();
        Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(inputStream);
        doc.getDocumentElement().normalize();
        inputMap.put(DB_URL, ((Element) doc.getElementsByTagName(DB_URL).item(0)).getTextContent());
        inputMap.put(DB_USER, ((Element) doc.getElementsByTagName(DB_USER).item(0)).getTextContent());
        inputMap.put(DB_PASS_KEY, ((Element) doc.getElementsByTagName(DB_PASS_KEY).item(0)).getTextContent());
        inputMap.put(DRIVER, ((Element) doc.getElementsByTagName(DRIVER).item(0)).getTextContent());
        inputMap.put(SIZE, ((Element) doc.getElementsByTagName(SIZE).item(0)).getTextContent());
        inputMap.put(TCOUNT, ((Element) doc.getElementsByTagName(TCOUNT).item(0)).getTextContent());
        inputMap.put(SQL, ((Element) doc.getElementsByTagName(SQL).item(0)).getTextContent());
        return inputMap;
    }
}

答案 1 :(得分:0)

如果您不排除非基于jaxb的解决方案......这是使用XPath和VTD-XML进行此操作的一种方法。这样做的好处是你可以将尽可能多的元素/值对放入文件中,它仍然可以工作......零配置......

catch

答案 2 :(得分:0)

如果您使用的是Java 8,则可以尝试使用XmlDynamic

XmlDynamic xml = new XmlDynamic("<InputData>" + // the xml
    "  <dbproperties>" +
    "    <dburl>dbURL</dburl>" +
    "    <dbuser>DBUSER</dbuser>" +
    "    <dbpasskey>DBPASS</dbpasskey>" +
    "    <driver>oracle.jdbc.driver.OracleDriver</driver>" +
    "  </dbproperties>" +
    "  <tdata>" +
    "    <size>4</size>    " +
    "    <tcount>6</tcount>" +
    "  </tdata>" +
    "  <qdata>" +
    "    <sql>select syadate from dual;</sql>" +
    "  </qdata>" +
    "</InputData>");

xml.get("InputData").children()
    .flatMap(Dynamic::children)
    .forEach(element -> System.out.println(element.key().asString() + " = " + element.asString()));

这将打印出所有深度2标签名称(键)和内容,即在这种情况下:

dburl = dbURL
dbuser = DBUSER
dbpasskey = DBPASS
driver = oracle.jdbc.driver.OracleDriver
size = 4
tcount = 6
sql = select syadate from dual;

请参阅https://github.com/alexheretic/dynamics#xml-dynamics