aspose file tools
The moose likes Java in General and the fly likes Using a parsed XML document Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Using a parsed XML document" Watch "Using a parsed XML document" New topic
Author

Using a parsed XML document

Sumon Mukherjee
Greenhorn

Joined: Dec 19, 2007
Posts: 12
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
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12271
    
    1
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


Java Resources at www.wbrogden.com
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 16483
    
    2

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

Joined: Dec 19, 2007
Posts: 12
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
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: Using a parsed XML document
 
Similar Threads
not able to create XML through DOM...
Sending requests in xml
need urgent help on JSP and XMl PLEASE!!!!!!
DOM (xml) question
Getting the arraylist through session