Better way to parse xml

前端 未结 9 2109
感动是毒
感动是毒 2021-02-06 05:05

I\'ve been parsing XML like this for years, and I have to admit when the number of different element becomes larger I find it a bit boring and exhausting to do, here is what I m

9条回答
  •  野趣味
    野趣味 (楼主)
    2021-02-06 05:41

    There is another library which supports more compact XML parsing, RTXML. The library and its documentation is on rasmustorkel.com. I implemented the parsing of the file in the original question and I am including the complete program here:

    package for_so;
    
    import java.io.File;
    import java.util.ArrayList;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    
    import rasmus_torkel.xml_basic.read.TagNode;
    import rasmus_torkel.xml_basic.read.XmlReadOptions;
    import rasmus_torkel.xml_basic.read.impl.XmlReader;
    
    public class Q15626686_ReadOrder
    {
        public static class Order
        {
            public final Date            _date;
            public final int             _customerId;
            public final String          _customerName;
            public final ArrayList _itemAl;
    
            public
            Order(TagNode node)
            {
                _date = (Date)node.nextStringMappedFieldE("Date", Date.class);
                _customerId = (int)node.nextIntFieldE("CustomerId");
                _customerName = node.nextTextFieldE("CustomerName");
                _itemAl = new ArrayList();
                boolean finished = false;
                while (!finished)
                {
                    TagNode itemNode = node.nextChildN("Item");
                    if (itemNode != null)
                    {
                        Item item = new Item(itemNode);
                        _itemAl.add(item);
                    }
                    else
                    {
                        finished = true;
                    }
                }
                node.verifyNoMoreChildren();
            }
        }
    
        public static final Pattern DATE_PATTERN = Pattern.compile("^(\\d\\d\\d\\d)\\/(\\d\\d)\\/(\\d\\d)$");
    
        public static class Date
        {
            public final String _dateString;
            public final int    _year;
            public final int    _month;
            public final int    _day;
    
            public
            Date(String dateString)
            {
                _dateString = dateString;
                Matcher matcher = DATE_PATTERN.matcher(dateString);
                if (!matcher.matches())
                {
                    throw new RuntimeException(dateString + " does not match pattern " + DATE_PATTERN.pattern());
                }
                _year = Integer.parseInt(matcher.group(1));
                _month = Integer.parseInt(matcher.group(2));
                _day = Integer.parseInt(matcher.group(3));
            }
        }
    
        public static class Item
        {
            public final int      _itemId;
            public final String   _itemName;
            public final Quantity _quantity;
    
            public
            Item(TagNode node)
            {
                _itemId = node.nextIntFieldE("ItemId");
                _itemName = node.nextTextFieldE("ItemName");
                _quantity = new Quantity(node.nextChildE("Quantity"));
                node.verifyNoMoreChildren();
            }
        }
    
        public static class Quantity
        {
            public final int _unitSize;
            public final int _unitQuantity;
    
            public
            Quantity(TagNode node)
            {
                _unitSize = node.attributeIntD("unit", 1);
                _unitQuantity = node.onlyInt();
            }
        }
    
        public static void
        main(String[] args)
        {
            File xmlFile = new File(args[0]);
            TagNode orderNode = XmlReader.xmlFileToRoot(xmlFile, "Order", XmlReadOptions.DEFAULT);
            Order order = new Order(orderNode);
            System.out.println("Read order for " + order._customerName + " which has " + order._itemAl.size() + " items");
        }
    }
    

    You will notice that the retrieval functions end in N, E or D. They refer to what to do when the desired data item is not there. N stands for return Null, E stands for throw Exception and D stands for use Default.

提交回复
热议问题