1

I want to parse "ALışVERIş" type of content from service.

SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader(); 
myXMLHandler = new XMLHandler();
xr.setContentHandler(myXMLHandler); 
URL _url = new URL(params[0]); 
 xr.parse(new InputSource(_url.openStream()));
Jenefer
  • 113
  • 8
  • Could you explain more what's the problem with your code? What's not working? What's current result and what's the expected result? – Andrew T. Mar 12 '14 at 06:00
  • I had done parsing the string from service it works fine for normal string value.but it cant able to display special characters present in service. [link](http://182.160.161.2/~siva/turkish/web_serv/category.php?order_by=desc&category_id=2).can u guide me on doing this. – Jenefer Mar 12 '14 at 06:06
  • I have checked your link, I tried it with DOM parser, and it's working fine, I can read those turkish words too and display it on textview. :) – TheLittleNaruto Mar 12 '14 at 06:44

1 Answers1

1

What I tried DOM Parser instead of XaxParser. Check below function and call it inside background thread :

public String readXML(){
        StringBuilder stringBuilder = new StringBuilder();
         try {
                URL _url = new URL("http://182.160.161.2/~siva/turkish/web_serv/category.php?order_by=desc&category_id=2"); 
                File fXmlFile = new File(new InputSource(_url.openStream()).getByteStream().toString());
                DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
                Document doc = dBuilder.parse(new InputSource(_url.openStream()).getByteStream());

                //optional, but recommended
                //read this - http://stackoverflow.com/questions/13786607/normalization-in-dom-parsing-with-java-how-does-it-work
                doc.getDocumentElement().normalize();

                System.out.println("Root element :" + doc.getDocumentElement().getNodeName());

                NodeList nList = doc.getElementsByTagName("document");

                Node nResp = nList.item(0);
                Element fstElmnt = (Element) nResp;
                NodeList nameList1 = fstElmnt.getElementsByTagName("response");

                Node mCat = nameList1.item(0);
                Element catElmt = (Element) mCat;

                NodeList catList = catElmt.getElementsByTagName("category");

                System.out.println("----------------------------");

                for (int temp = 0; temp < catList.getLength(); temp++) {

                    Node nNode = catList.item(temp);

                    System.out.println("\nCurrent Element :" + nNode.getNodeName());

                    if (nNode.getNodeType() == Node.ELEMENT_NODE) {

                        Element eElement = (Element) nNode;

                        stringBuilder.append("Category ID : " + eElement.getElementsByTagName("category_name").item(0).getTextContent()+"\n");
                        stringBuilder.append("Category Name : " + eElement.getElementsByTagName("category_id").item(0).getTextContent());
                        System.out.println("Category ID : " + eElement.getElementsByTagName("category_name").item(0).getTextContent());
                        System.out.println("Category Name : " + eElement.getElementsByTagName("category_id").item(0).getTextContent());


                    }
                }
                } catch (Exception e) {
                e.printStackTrace();
                }
              return stringBuilder.toString();

    }

Here is a screenshot of my device, Have a look : You can see those turkish language too :)

enter image description here

TheLittleNaruto
  • 8,325
  • 4
  • 54
  • 73
  • I should have mentioned that in the answer, instead of calling getNodeValue(), I have called getTextContent(). That did the task. :) – TheLittleNaruto Mar 12 '14 at 07:32