Do not use on any
shared computer
September 7, 2008 12:52am pdt
 
[x]
Attachment Details

What is wrong with this Servlet? It keeps telling me a method is undefined when it very clearly is....

Hello,
I have a servlet which renders xml data, and sends it out over http. I have built an object called XMLStructure which allows me to create the xml via functions, and then dump the xml text to various formats. Anyways, when I hit the servlet, I get this error:

StandardWrapperValve[xmlDataServices]: PWC1406: Servlet.service() for servlet xmlDataServices threw exception
java.lang.NoSuchMethodError: XMLStructure.getXMLText()Ljava/lang/String;
        at xmlDataServices.createServerData(xmlDataServices.java:247)
        at xmlDataServices.processRequest(xmlDataServices.java:89)
        at xmlDataServices.doPost(xmlDataServices.java:332)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:738)
        at javax.servlet.http.HttpServlet.service(HttpServlet.java:831)

Now normally this would mean that the getXMLText() function im using doesnt  exist... but here's the thing. It very clearly does... see the class below.

Here is an example of the usage, and where the error occurs:


XMLStructure serverData = new XMLStructure();

serverData.putValue("root/data","some data");

out.print(serverData.getXMLText()); // <!---- this is the equivalent of the line that throws the exception


A correct fix for this problem is worth 500 points.

Thanks,
Rick
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
65:
66:
67:
68:
69:
70:
71:
72:
73:
74:
75:
76:
77:
78:
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92:
93:
94:
95:
96:
97:
98:
99:
100:
101:
102:
103:
104:
105:
106:
107:
108:
109:
110:
111:
112:
113:
114:
115:
116:
117:
118:
119:
120:
121:
122:
123:
124:
125:
126:
127:
128:
129:
130:
131:
132:
133:
134:
135:
136:
137:
138:
139:
140:
141:
142:
143:
144:
145:
146:
147:
148:
149:
150:
151:
152:
153:
154:
155:
156:
157:
158:
159:
160:
161:
162:
163:
164:
165:
166:
167:
168:
169:
170:
171:
172:
173:
174:
175:
176:
177:
178:
179:
180:
181:
182:
183:
184:
185:
186:
187:
188:
189:
190:
191:
192:
193:
194:
195:
196:
197:
198:
199:
200:
201:
202:
203:
204:
205:
206:
207:
208:
209:
210:
211:
212:
213:
214:
215:
216:
217:
218:
219:
220:
221:
222:
223:
224:
225:
226:
227:
228:
229:
230:
231:
232:
233:
234:
235:
236:
237:
238:
239:
240:
241:
242:
243:
244:
245:
246:
247:
248:
249:
250:
251:
252:
253:
254:
255:
256:
257:
258:
259:
260:
261:
262:
263:
264:
265:
266:
267:
268:
269:
270:
271:
272:
273:
274:
275:
276:
277:
278:
279:
280:
281:
282:
283:
284:
285:
286:
287:
288:
289:
290:
291:
292:
293:
294:
295:
296:
297:
298:
299:
300:
301:
302:
303:
304:
/*
 * XMLStructure.java
 *
 * Created on April 8, 2007, 6:52 PM
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */
 
 
 
//VoipTrix BroadcastPBX 
import com.sun.org.apache.xml.internal.serialize.OutputFormat;
import com.sun.org.apache.xml.internal.serialize.XMLSerializer;
import javax.xml.parsers.DocumentBuilderFactory;  
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;  
import org.w3c.dom.*;
import javax.xml.xpath.*;
import java.util.*;
import javax.xml.transform.*;
import org.xml.sax.*;
import javax.xml.parsers.*;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;
import java.net.*;
import java.io.*;
 
/**
 *
 * @author Rick
 */
public class XMLStructure
{
    private Document data;
    
    /** Creates a new instance of XMLStructure */
    public XMLStructure()
    {
        //create a new Blank Structure
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();    	
    	try 
        {
            DocumentBuilder builder = factory.newDocumentBuilder();
            data = builder.newDocument();
            Element root = data.createElement("root");
            data.appendChild(root);
            
	 }
	 catch (ParserConfigurationException pce) 
         {
	       // Parser with specified options can't be built
	       System.out.println("Error initializing config.xml parser. " + pce.getMessage());
	 }
    }
    
    public XMLStructure(String xml)
    {
        //create a structure from the passed XML
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    	
        //System.out.println("XML DATA:\n\n" + xml);
        
                try 
                {
                    DocumentBuilder builder = factory.newDocumentBuilder();
                    data = builder.parse(new ByteArrayInputStream(xml.getBytes()));
                }
		catch (SAXException sxe) 
		{
                    System.out.println("XMLSAX:" + sxe.getMessage());
		} 
		catch (ParserConfigurationException pce) 
		{
                    System.out.println("XMLPCE:" + pce.getMessage());
                }
		catch (IOException ioe)
		{
                    System.out.println("IO:" + ioe.getMessage());
                }
    }
    
    public String getXMLText()
    {
        //returns the xml structure as a URL Encoded String
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        
        StringWriter stringOut = new StringWriter();
        
        Result r = new StreamResult(stringOut);
        
        try
        {
            Transformer serializer = transformerFactory.newTransformer();
            serializer.transform(new DOMSource(data),r);
        }
        catch (Exception e)
        {
            System.out.println("Exception converting XML.\n\n\n");
        }
        
        return stringOut.toString();
    }
    
    public String getValueList(String key)
    {
    	String valuelist="";
    	
    	try
    	{
    		 XPath xpath = XPathFactory.newInstance().newXPath();
    		 NodeList nodeList = (NodeList) xpath.evaluate(key, data, XPathConstants.NODESET);
    		 for (int i = 0; i < nodeList.getLength(); i++)
    			 valuelist = valuelist + nodeList.item(i).getNodeName() + ",";
    	}
    	catch (XPathExpressionException e) 
        {
		//System.out.println("Error while retrieving valuelist for " + key);
		valuelist = null;
	}
    	
        if (valuelist.length() > 0)
            return valuelist.substring(0,valuelist.length()-1);
        else
            return "";
    }
    
    private Node getNode(String path) throws Exception
    {
        XPath xpath = XPathFactory.newInstance().newXPath();
        Node widgetNode = (Node) xpath.evaluate(path, data, XPathConstants.NODE);
        return widgetNode;
    }
    
    public boolean pathExists(String path)
    {
        try
        {
            XPath xpath = XPathFactory.newInstance().newXPath();
            return ((Node) xpath.evaluate(path, data, XPathConstants.NODE) != null);
        }
        catch(Exception e)
        {
            return false;
        }
    }
    
     public void removeValue(String xpath)
    {
        //remove a reference in the XML tree
        try
        {
           Node node = getNode(xpath);
           node.getParentNode().removeChild(node);
        }
        catch (Exception e)
        {
        }
    }
    
     public void putValue(String path, String value)
    {
         try
         {
          if (pathExists(path))
          {
              String keys[] = path.split("/");
              Element newNode = data.createElement(keys[keys.length-1]);
              newNode.setTextContent(value.replaceAll("\\\\","/"));
              Node node = getNode(generateNewPath(keys,keys.length-2));
              node.replaceChild(newNode,getNode(path));
          }
          else
          {
              //the path doesnt exist so lets drill down and add them as we need to
              String keys[] = path.split("/");
              
              for (int i = 0; i < keys.length; i++)
                  if (!pathExists(generateNewPath(keys,i)))
                  {
                        //create the new node
                        Element newNode = data.createElement(keys[i]);
                        Node node = getNode(generateNewPath(keys,i-1));
                        if (i == (keys.length - 1))
                            newNode.setTextContent(value.replaceAll("\\\\","/"));
                        node.appendChild((Node) newNode);
                  }
          }
         }
         catch(Exception e)
         {
            System.out.println("There was an error putting the config value.\n " + e.getMessage());
         }
    }
    
    private String generateNewPath(String nodes[],int index)
    {
        //generate a new xmlpath based on the nodes array and index
        String retVal = "";
        retVal = nodes[0];
        for (int j = 1; j <= index; j++)
           retVal += "/" + nodes[j];      
        return retVal;
    }
 
    public String getValue(String key)
    {
	String value="";
	//return the value associated with given XML Path
	try 
	{
            value=(getNode(key)).getFirstChild().getNodeValue();
        } 
        catch (Exception e) 
        {
	}	
        return value;
    }
     
    public Hashtable getValueHash(String key)
    {
        Hashtable retVal = new Hashtable();
        
        //get a list of all the corresponding values in this record
        String[] columns = getValueList(key + "/*").split(",");
        
        for (int i = 0; i < columns.length; i++)
            retVal.put(columns[i],getValue(key + "/" + columns[i]));
        
        return retVal;
    }
 
    public String getXMLURLText()
    {
        //returns the xml structure as a URL Encoded String
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        
        StringWriter stringOut = new StringWriter();
        
        Result r = new StreamResult(stringOut);
        
        try
        {
            Transformer serializer = transformerFactory.newTransformer();
            serializer.transform(new DOMSource(data),r);
        }
        catch (Exception e)
        {
            System.out.println("Exception converting XML.\n\n\n");
        }
        
        return URLEncoder.encode(stringOut.toString());
    }
    
     public boolean loadXML(String filetoload)
	{
    	DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    	boolean error = true;
    	
    	try 
	{
			DocumentBuilder builder = factory.newDocumentBuilder();
			data = builder.parse(new File(filetoload));	
	}
	catch (SAXException sxe) 
	{
		System.out.println("Invalid XML construct found in config.xml. " + sxe.getMessage());
		error = false;
	} 
	catch (ParserConfigurationException pce) 
	{
	      // Parser with specified options can't be built
	      System.out.println("Error initializing config.xml parser. " + pce.getMessage());
	      error = false;
	}
	catch (IOException ioe)
	{
	       // I/O error
	       System.out.println(ioe.getMessage());
	       error=false;
	}
	    		
	    return error;
	}
    
   public void writeXML(String filetowrite)
   {
           try 
           {
            // Prepare the DOM document for writing
            Source source = new DOMSource(data);
    
            // Prepare the output file
            File file = new File(filetowrite);
            Result result = new StreamResult(file);
    
            // Write the DOM document to the file
            Transformer xformer = TransformerFactory.newInstance().newTransformer();
            xformer.transform(source, result);
           } catch (TransformerConfigurationException e) {
           } catch (TransformerException e) {
        }
   }
}
Start your free trial to view this solution
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

Question Stats
Zone: Programming
Question Asked By: richardsimnett
Solution Provided By: Bart_Cr
Participating Experts: 2
Solution Grade: A
Views: 0
Translate:
Loading Advertisement...
 
[+][-]Accepted Solution by Bart_Cr

Rank: Guru

Accepted Solution by Bart_Cr:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
[+][-]Expert Comment by ragerino
Expert Comment by ragerino:

All comments and solutions are available to Premium Service Members only.

Start your 7-day free trial and see for yourself why Experts Exchange is the easiest and most proven technology resource in the world. Get Started

Already a member? Login to view this solution.

 
 
Loading Advertisement...
20080723-EE-VQP-34 / EE_QW_2_20070628