1

I have to parse this XML https://i.stack.imgur.com/YrIzS.jpg to extract the highlighted string under the "DetailPageURL" tag. I did a try but have quite confused ideas. I'm working on Android SDK

import android.os.AsyncTask;

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

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URL;

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


public class ReflinkFetcher extends AsyncTask<String, Void, String> {

String refLink = null;

@Override
protected String doInBackground( String... url){
    Document doc = openXML(url[0]);
    Element rootElement = doc.getDocumentElement();
    refLink = getString("DetailPageUrl", rootElement);
    return refLink;
}


public String getRefLink(){
    return refLink;
}

private Document openXML(String url) {
    Document doc = null;
    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        doc = db.parse(new URL(url).openStream());
    } catch (SAXException e1) {
        e1.printStackTrace();
    } catch (ParserConfigurationException e1) {
        e1.printStackTrace();
    } catch (MalformedURLException e2) {
        e2.printStackTrace();
    } catch (IOException e3) {
        e3.printStackTrace();
    }
    return doc;
}

private String getString(String tagName, Element element) {
    NodeList list = element.getElementsByTagName("Items");
    if (list != null && list.getLength() > 0) {
        NodeList subList = list.item(0).getChildNodes(); //0=items
        if (subList != null && subList.getLength() > 0) {
            subList = subList.item(4).getChildNodes(); //4=item
            if( subList != null && subList.getLength() >0 ) {
                subList = subList.item(2).getChildNodes(); //2=DetailPageURL
                if( subList != null && subList.getLength() >0 ) {
                    return subList.item(0).getNodeValue(); // Value??
                }

            }
        }
    }
    return null;
}

}

The final return null is being executed. I think the "getString" method is quite poor too. How can I improve it?

Alessandro
  • 13
  • 2
  • I recommend you to read about Retrofit and Simple XML Converter for it – DeKaNszn Jul 28 '17 at 13:28
  • This generic question has been answered many other times before - e.g. https://stackoverflow.com/questions/340787/parsing-xml-with-xpath-in-java The solution given there uses XPath, which I think would be the ideal solution for this specific problem. – Squiggle Jul 28 '17 at 14:12
  • Possible duplicate of [Parsing XML with XPath in Java](https://stackoverflow.com/questions/340787/parsing-xml-with-xpath-in-java) – Squiggle Jul 28 '17 at 14:12
  • Possible duplicate of [How to read XML using XPath in Java](https://stackoverflow.com/questions/2811001/how-to-read-xml-using-xpath-in-java) – eshirima Jul 28 '17 at 15:14

1 Answers1

0

Check this nice open source library, which do all you need is nice XML parser.

chebad
  • 919
  • 1
  • 13
  • 29
  • It looks like a good idea. However what if I have a remote URL pointing to an XML file? Are there better ideas than getting the xml in a org.w3c.Document and converting it to a String to use with the parser you linked? – Alessandro Jul 28 '17 at 18:06