aspose file tools*
The moose likes XML and Related Technologies and the fly likes How to get an XML field? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "How to get an XML field? " Watch "How to get an XML field? " New topic
Author

How to get an XML field?

Raj S Kumar
Ranch Hand

Joined: Aug 06, 2006
Posts: 48
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?


Raj S Kumar
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12806
    
    5

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

Joined: Jun 02, 2010
Posts: 13
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

Joined: Mar 22, 2000
Posts: 12806
    
    5
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

Joined: Jun 02, 2010
Posts: 13
Sorry for that.. well I did exactly the same. Got my expected result..

Thanks a lot.
Abhijeet
Raj S Kumar
Ranch Hand

Joined: Aug 06, 2006
Posts: 48
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

Joined: Mar 22, 2000
Posts: 12806
    
    5
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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to get an XML field?