0

I want to parse a very long string from an xml file. You can see the xml file here. If you visit the above file, there is a "description" tag from which I want to parse the string. When there is a short short string, say 3-lines or 4-lines string in the "description" tag, then my parser(Java SAX parser) easily parse the string but, when the string is hundreds of lines then my parser cannot parse the string. You can check my code that I am using for the parsing and please let me know where I am going wrong in this regard. Please help me in this respect I would be very thankful to you for this act of kindness.

Here is the parser GetterSetter class

public class MyGetterSetter 
{
    private ArrayList<String> description = new ArrayList<String>();


        public ArrayList<String> getDescription()
        { 
            return description;
        }

        public void setDescription(String description) 
        { 


            this.description.add(description);
        }
} 

Here is the parser Handler class

public class MyHandler extends DefaultHandler 
{
    String elementValue = null;
    Boolean elementOn = false;
    Boolean item = false;

    public static MyGetterSetter data = null;

    public static MyGetterSetter getXMLData() 
    {
        return data;
    }

    public static void setXMLData(MyGetterSetter data) 
    {
        MyHandler.data = data;
    }


    public void startDocument() throws SAXException
    {
        data =  new MyGetterSetter();
    }

    public void endDocument() throws SAXException
    {

    }

    public void startElement(String namespaceURI, String localName,String qName, Attributes atts) throws SAXException
    {
        elementOn = true;

        if (localName.equalsIgnoreCase("item"))
        item = true;
    }

    public void endElement(String namespaceURI, String localName, String qName) throws SAXException
    {
        elementOn = false;

        if(item)
        {

            if (localName.equalsIgnoreCase("description"))
                {   
                data.setDescription(elementValue);


                Log.d("--------DESCRIPTION------", elementValue +" ");

                }


            else if (localName.equalsIgnoreCase("item")) item = false;
        }



    }

    public void characters(char ch[], int start, int length)
    {
        if (elementOn) 
        {
            elementValue = new String(ch, start, length);
            elementOn = false;
        }
    }



}
4

1 回答 1

0

使用 org.w3c.dom 包。

public static void main(String[] args) {
    try {
        URL url = new URL("http://www.aboutsports.co.uk/fixtures/");

        DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
        Document doc = dBuilder.parse(url.openStream());

        NodeList list = doc.getElementsByTagName("item"); // get <item> nodes

        for (int i = 0; i < list.getLength(); i++) {
            Node item = list.item(i);
            NodeList descriptions = ((Element)item).getElementsByTagName("description"); // get <description> nodes within an <item>
            for (int j = 0; j < descriptions.getLength(); j++) {
                Node description = descriptions.item(0);

                System.out.println(description.getTextContent()); // print the text content
            }
        }

    } catch (Exception e) {
        e.printStackTrace();
    }
}

Java 中的XPath也非常适合从 XML 文档中提取位。这是一个例子。

你会使用XPathExpressionlike /item/description。当您在 XML 上评估它时InputStream,它会返回一个NodeList类似的<description>元素,其中包含一个元素中的所有<item>元素。

如果您想按照自己的方式使用 a DefaultHandler,则需要设置和取消设置标志,以便检查您是否在<document>元素的主体中。上面的代码可能在内部做了类似的事情,对你隐藏了它。该代码在java中可用,为什么不使用它呢?

于 2013-05-22T20:01:38.450 回答