• 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 ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Tim Cooke
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • paul wheaton
  • Devaka Cooray
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Ron McLeod
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Tim Holloway
  • Carey Brown
  • salvin francis

Using a parsed XML document

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

Greetings!

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);
doc.getDocumentElement().normalize();
}
catch(Exception e){
e.printStackTrace();
}
return(doc);
}
************************************

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,
Sumon
 
Author and all-around good cowpoke
Posts: 13078
6
  • 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.

Bill
 
Sheriff
Posts: 24635
56
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And:

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
Greenhorn
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-
<header>
<presentation role="category" label="Category">
Directory Services
</presentation>
</header>

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;

xmlDoc.getDocumentElement().normalize();

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++){

if(childAttr.getNamedItem(strAttr).getTextContent().equals(strAttrValue)){
childNode.setTextContent(newVal);
break;
}
}
}

}
}
}

<<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.

Cheers,
Sumon
 
roses are red, violets are blue. Some poems rhyme and some are a tiny ad:
professionally read, modify and write PDF files from Java
https://products.aspose.com/pdf/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!