This week's book giveaway is in the Artificial Intelligence and Machine Learning forum.
We're giving away four copies of TensorFlow 2.0 in Action and have Thushan Ganegedara on-line!
See this thread for details.
Win a copy of TensorFlow 2.0 in Action this week in the Artificial Intelligence and Machine Learning forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
  • Campbell Ritchie
  • Liutauras Vilda
  • Paul Clapham
  • Bear Bibeault
  • Jeanne Boyarsky
  • Ron McLeod
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Jj Roberts
  • Stephan van Hulst
  • Carey Brown
  • salvin francis
  • Scott Selikoff
  • fred rosenberger

Using a parsed XML document

Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello All,


I am trying to play around with a XML document. In my java code -

I pass the XML document path from my JSP. I am creating a FILE object and then using DocumentBuilderFactory, DocumentBuilder I parse the XML into a Document object. Code snippet-

public static Document RetrieveXML(String filename) {
String ret = null;
Document doc;
try {
File file = new File(filename);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
doc = db.parse(file);
catch(Exception e){

Now in my RetrieveXML method, I want to return the Document object so that I can pass it to other methods from my jsp for setting/getting Element value, attribute value. For eg: In this method I want to set the value of an element. I am passing the parsed XML document from above.

public static void setElementValue(Document xmldoc,String strElement,String strValue){}

The problem is that, RetrieveXML does not return the Document object. It is throwing an error. Any idea why?
Is my approach wrong? Any help in this regard will be highly appreciated!

Thanks in Advance,
Best Wishes,
Author and all-around good cowpoke
Posts: 13078
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

RetrieveXML does not return the Document object. It is throwing an error. Any idea why?

My crystal ball is in the shop for the 10,000 vision checkup so I can't mystically visualize your exact exception and stack trace. You will have to provide them in your next post.

If I had to bet, I would bet the problem lies in your use of "filename" and the resulting File object. Unless that String is a complete path to the file, File is not referring to an actual file thus breaking the db.parse( file) statement. I suggest:

1. Create a helper class to do ALL of your document manipulation, with one instance of the helper per xml document - NOT static methods.
2. Make it something that can be tested outside the JSP environment.
3. Specifically check that the File object corresponds to a real readable file before trying to parse.
4. Use set and get JavaBean conventions to modify / read the document.

Posts: 25969
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

5. Make sure you understand that your JSP is running on the server and cannot access files that are on the client computer.
Sumon Mukherjee
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
William and Paul...thanks a lot for your replies.

I tried something like this -

My XML file looks like this-
<presentation role="category" label="Category">
Directory Services

I am trying to replace the text of this element.

This is my function -

public static String setAttrib(Document xmlDoc,String strTag,String strElement,String strAttr,String strAttrValue,String newVal)
String retu = null;
Node n = null;
Node fstNode = null;
Node childNode = null;
int c = 0;


NodeList nodeLst = xmlDoc.getElementsByTagName(strTag);

for (int s = 0; s < nodeLst.getLength(); s++) {
fstNode = nodeLst.item(s);
NodeList childNodeLst = fstNode.getChildNodes();
for (int p = 0; p < childNodeLst.getLength(); p++) {
childNode = childNodeLst.item(p);

if (childNode.getNodeName().equalsIgnoreCase(strElement)){
NamedNodeMap childAttr = childNode.getAttributes();
for ( int q = 0; q < childAttr.getLength(); q++){



<<Sorry for pasting it in this format as all the indentations were getting jumbled>>

Document XMLDoc is a parsed XML doc.

Now my method is changing the value. But it is doing so in the memory.

Any idea how I can write it to the XML file? Should i use StringBuffer class or DOM. If so how?

Thanks for your help.

Look! It's Leonardo da Vinci! And he brought a tiny ad!
Thread Boost feature
    Bookmark Topic Watch Topic
  • New Topic