如何从配置文件中编写和获取xml值

时间:2018-04-25 18:00:06

标签: java xml webobjects

我需要获取xml 标签值,具体取决于 id ,每个ID都有相应的标签值,我需要将其显示到我的webobject页面。 xml详细信息将放在配置文件中,并将其调用以获取标签值。

sample.xml中

<?xml version="1.0" encoding="UTF-8"?>

<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">

-<properties>

<comment>This is how we can have properties as xml</comment>

<entry key="shipping_method_id">**0**</entry>

<entry key="shipping_method_label">Fixed Shipping</entry>

<entry key="shipping_method_id">0</entry>

<entry key="shipping_method_label">**Fixed Shipping**</entry>

</properties>

下面是我获取其他标签的java代码。我如何调用方法标签?谢谢你的帮助!

public NSDictionary parseShipment(String shipString) {
        String carrier = extractString(shipString, "<enum>", "</enum>", 0);
        String dateString = extractString(shipString, "<date>", "</date>", 0);
        String trackingNumber = extractString(shipString, "<track>", "</track>", 0);
        String oms = extractString(shipString, "<status>", "</status>", 0);

        if (carrier == null) carrier = "";
        if (trackingNumber == null) trackingNumber = "";
        if (dateString == null) dateString = "";
        if (oms == null) oms = "";

        // parse the ship date
        NSTimestamp shipdate = null;
        int markerIndex = dateString.indexOf("T");
        if (markerIndex > 0) {
            dateString = dateString.substring(0, markerIndex);
            try { shipdate = (NSTimestamp) dateformatter.parseObject(dateString); }
            catch (ParseException e) { }
        }

        NSMutableDictionary returnDict = new NSMutableDictionary();
        returnDict.setObjectForKey(carrier, "carrier");
        returnDict.setObjectForKey(trackingNumber, "trackingNumber");
        returnDict.setObjectForKey(oms, "oms");
        System.out.println(oms);
        if (shipdate == null)
            returnDict.setObjectForKey(dateString, "shipDatetime");
        else
            returnDict.setObjectForKey(shipdate, "shipDatetime");

1 个答案:

答案 0 :(得分:0)

最可靠的方法是使用实​​际的XML解析器。有几十个(甚至JDK本身有一个),还有成千上万的文章描述了如何做到这一点。这是relatively recent one