How to write and get the xml values from a config file

751 views Asked by At

I need to get the xml label value depending on the id, each id has a corresponding label value and i need to display it to my webobject page. the xml details will be place on a config file and will call it to get the label values.

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>

below is my java code to get the other tags. How will I call the method label? thanks for your help!

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

There are 1 answers

0
Paul A. Hoadley On

The most reliable way will be to use an actual XML parser. There are dozens of these (even the JDK itself has one), and thousands of articles describing how to do it. Here is a relatively recent one.