Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How to get an XML field?

 
Raj S Kumar
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the following XML file listing and it needs to be parsed.




To parse this, I am using DOMParser with the following code.



I want to read the tag <tempName> and doing this @ line no. 11.

Is there any other way to retrieve the tag by Tagname rather than its position?
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13064
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

You should never depend on the numbering of NodeList items - those extra formatting Text nodes cause lots of trouble.

Why are you getting the "Files" document root? Why not the "file" repeating element?

Bill
 
Abhijeet Mohanty
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hey Guys,

I want to get the root element of an xml doc. and for the same I'm using the below methods which is giving me perfect result. But as per the changes in requirement I won't be getting any xml document anymore rather would receive just a xml String from the upstream. In that case how can I get the root element??? Please suggest or let me know if you need further details. Kindly reply, Urgent!!!

private XMLElement getRootElement(String xmlFile) {
XMLDocument xmldoc = getDocument(xmlFile);
if (xmldoc != null) {
return (XMLElement)xmldoc.getDocumentElement();
}
return null;
}



private XMLDocument getDocument(String xmlFile) {

XMLDocument doc = null;

try {
DOMParser parser = new DOMParser();

parser.showWarnings(true);

InputStream is = null;
Object externalContext =
FacesContext.getCurrentInstance().getExternalContext().getContext();
if (externalContext instanceof ServletContext) {
is = this.getClass().getResourceAsStream(xmlFile);
if (is != null) {
parser.parse(is);
// Obtain the document
doc = parser.getDocument();
}
}

} catch (IOException ioe) {

ioe.printStackTrace();

} catch (SAXException saxe) {

saxe.printStackTrace();

}

return doc;

}
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13064
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
1. start a new topic instead of hijacking old one
2. use formatting for code

If the String is formatted as an XML document - create a java.io.StringReader to get your input stream
If the String is not formatted with a root element you will have to add one.
Bill
 
Abhijeet Mohanty
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry for that.. well I did exactly the same. Got my expected result..

Thanks a lot.
Abhijeet
 
Raj S Kumar
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Bill,
I want to find an alternative way instead of using the numbering of NodeList items. Is there an alternative?

I am getting the "Files" because, there might be some other tags with the same name "file". I want to collect only the "file" elements in the "Files" tag.
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13064
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
With a org.w3c.dom.NodeList you are always going to be using the index because that is the only method provided.

The point I was trying to make is you should not assume that item(1) or any item(n) is always going to be what you want. You can check the node type - see org.w3c.dom.Node documentation for the constants used to identify node types.

Bill
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic