0

i have a class which is returning a string type value and i want to return an String array, so please tell how can i able to do that

  1. i have an xml file like resource.xml
<prompts> 
        <prompt id="p1">welcome to</prompt> 
        <prompt id ="p2">stack overflow</prompt>
        <prompt id="p3">You entered</prompt> 
        <prompt id="p4">the correct number</prompt> 
<prompts>

i am parsing it using sax parser

    public class XmlReaderPrompt {  
        public List<PromptBean> load(String langMode)
        {
            String fileName="resource.xml";     
            DocumentBuilderFactory dbf=DocumentBuilderFactory.newInstance();
            InputStream prompt_configfile=Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);
            DocumentBuilder db = null;
            List<PromptBean> promptMap = new ArrayList<PromptBean>();
            try {
                try {
                    db = dbf.newDocumentBuilder();
                } catch (ParserConfigurationException e) {              
                    e.printStackTrace();
                }
                Document doc = null;
                try {
                    doc = db.parse(prompt_configfile);
                }

                catch (SAXException e) {                
                    e.printStackTrace();
                }           
                NodeList nodeList=doc.getElementsByTagName("prompt");
                for(int i=0;i<nodeList.getLength();i++)
                {
                    Node node=nodeList.item(i);
                    if(node.getNodeType()==Node.ELEMENT_NODE)
                    {                                           
                        Element element=(Element)node;
                        String id = element.getAttribute("id");             
                        String name = element.getAttribute("name");                 
                        String prompt=getTextValue(element);
                        promptMap.add(new PromptBean(id,name,prompt));
                    }
                }
            }        
            catch(Exception io)
            {
                io.printStackTrace();           
            }
            finally
            {
                db=null;
                dbf=null;
            }       
            return promptMap;
        }   

        private String getTextValue(Element element) {      
            String textValue=element.getFirstChild().getTextContent().toString();
            return textValue;
        }
}

and a UserFunction class to return the text from the xml file

public class UserFunction{  

        List<PromptBean> promptObject = new ArrayList<PromptBean>(); 
public String getPromptFunction(String promptTag,String langMode )
        {           
            List<PromptBean> promptObject=xrpObject.load(langMode);
            for (Iterator<PromptBean> iterator = promptObject.iterator(); iterator.hasNext();){
            PromptBean promptBean= (PromptBean)iterator.next();             
            if(promptBean.getId().equalsIgnoreCase(promptTag)){
                return StringEscapeUtils.escapeXml(promptBean.getPrompt());
            }
            }
             return null;
        }

The problem is that I have to call the method getPromptFunction of UserFunction class every time I need to get text from the sub element like

String pr1 = UserFunction.getPromptFunction("p1" "resource");
String pr1 = UserFunction.getPromptFunction("p2" "resource");
String pr1 = UserFunction.getPromptFunction("p3" "resource");

and using it in jsp page as <%=pr1%>

So I want to use array like

String[] pr = UserFunction.getPromptFunction('"p1","p2","p3"' "resource")

So how I am able to do that and also tell how to use it in jsp page .

2
  • That's not actually SAX parsing you're doing. Commented Oct 15, 2012 at 11:17
  • Sorry for that but i want to know that how can i able to retrieve the text from XML in array list and print it in jsp page Commented Oct 17, 2012 at 8:54

1 Answer 1

1

You can do it like this

public String[] getPromptFunction(String promptTag,String langMode )
    {   
        String temp[] = new String[promptObject.size()];        
        List<PromptBean> promptObject=xrpObject.load(langMode);
        int i = 0; 
        for (Iterator<PromptBean> iterator = promptObject.iterator(); iterator.hasNext();) {
        PromptBean promptBean= (PromptBean)iterator.next();             
        if(promptBean.getId().equalsIgnoreCase(promptTag)){
            temp[i] = StringEscapeUtils.escapeXml(promptBean.getPrompt());
        }
        i++;
        }
        return temp;
    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.