File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Fail to access attributes from a DOM Document Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Fail to access attributes from a DOM Document" Watch "Fail to access attributes from a DOM Document" New topic
Author

Fail to access attributes from a DOM Document

miu san
Greenhorn

Joined: Aug 21, 2010
Posts: 4
Hi
This is a first for me ! i am a beginner in Java & am aiming to develop a servlet which will process the attributes & values of the nodes from an xml files url-encoded into a http POST request;
So far i am stuck when accessing the attributes of the nodes from the DOM Document that i had used to represent the xml file ...
Please find hereunder in red the bugging lines causing the error message "The type of expression must be an array type but it is resolved to NodeList":

public void acceptRepresentation(Representation entity) throws ResourceException {

log.info("Traitement d'un POST sur la ressource CreateSoundFilefromSSML");


// Récupération des paramètres du formulaire : le flux XML représentant
// une animation en découlera
Form form = new Form(entity);
String ssmlStr = form.getFirstValue("SSML");
String apiSigStr = form.getFirstValue("apisig");
String decodedSsmlString = URLDecoder.decode(ssmlStr, "UTF-8");

DocumentBuilderFactory ssmlFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder ssmlBuilder = null;
try{
ssmlBuilder = ssmlFactory.newDocumentBuilder();
Document ssmlDocument= ssmlBuilder.newDocument();
String voiceNameStr= ssmlDocument.getElementsByTagName("prosody")[0].childNodes[0].getAttribute("name");
String texttosyntStr= ssmlDocument.getElementsByTagName("prosody")[0].childNodes[0].nodeValue;
String audiosrcStr= ssmlDocument.getElementsByTagName("audio")[0].getAttribute("src");
String prosodyrateStr= ssmlDocument.getElementsByTagName("prosody")[0].getAttribute("rate");
String langStr= ssmlDocument.getElementsByTagName("speak")[0].getAttribute("xml:lang");


}catch(ParserConfigurationException pce) {
pce.printStackTrace();

}catch(SAXException se) {
se.printStackTrace();
}catch(IOException ioe) {
ioe.printStackTrace();
}


}

I know it must have something to do with the DOM Document declaration & methods calls but you could someone please help me to pinpoint what' wrong ?

Hoping it's not too much to ask ...
Thankx in advance !
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

In this line:

String voiceNameStr= ssmlDocument.getElementsByTagName("prosody")[0].childNodes[0].getAttribute("name");

you're trying to look at array elements in the objects returned by the getElementsByTagName() method. But those methods don't return arrays, they return instances of org.w3c.dom.NodeList , and you have to use the API of that class to access the objects. You could try to write it like this:

String voiceNameStr= ssmlDocument.getElementsByTagName("prosody").item(0).childNodes().item(0).getAttribute("name");

This wouldn't work, though, for several reasons. First, item() returns a Node, and getAttribute() is a member of Element. So actually, you'd need to write:

String voiceNameStr= ((Element) ssmlDocument.getElementsByTagName("prosody")).item(0). ...

And this wouldn't work, either, because neither Node nor Element has a method or attribute "childNodes" (although perhaps this is an extension provided by a particular XML implementation you're using?)

In any case, writing this kind of code all on one line is difficult for many reasons -- one is that when you get things wrong, as here, it can be hard to understand the error messages. The code is also prone to failure if the XML isn't quite what you expect.


[Jess in Action][AskingGoodQuestions]
miu san
Greenhorn

Joined: Aug 21, 2010
Posts: 4
Thank you indeed !
i've tried this instead which seems to be ok even though i still have anothe exception elsewhere ;p

DocumentBuilderFactory ssmlFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder ssmlBuilder = ssmlFactory.newDocumentBuilder();
Document ssmlDocument= ssmlBuilder.parse(new InputSource(new StringReader(decodedSsmlString)));

NodeList prosodyNd = ssmlDocument.getElementsByTagName("prosody");
Element prosodyEl = (Element)prosodyNd.item(0);
prosodyrateStr = prosodyEl.getAttribute("rate") ;

NodeList voiceNd = ssmlDocument.getElementsByTagName("voice");
Element voiceEl = (Element)voiceNd.item(0);
voiceNameStr = voiceEl.getAttribute("name") ;
texttosyntStr = voiceEl.getNodeValue();

NodeList audioNd = ssmlDocument.getElementsByTagName("audio");
Element audioEl = (Element)audioNd.item(0);
audiosrcStr = audioEl.getAttribute("src") ;

NodeList speakNd = ssmlDocument.getElementsByTagName("speak");
Element speakEl = (Element)speakNd.item(0);
langStr = speakEl.getAttribute("xml:lang") ;

Thankx !
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19719
    
  20

Please UseCodeTags next time.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
Rob Prime wrote:Please UseCodeTags next time.
. . . and indent your code. I would have added tags for you on your first post, but it wasn't indented, so I left it unchanged.
miu san
Greenhorn

Joined: Aug 21, 2010
Posts: 4
Duelly noted thkx for the advice!
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
Please don't write "thnx" or similar.
miu san
Greenhorn

Joined: Aug 21, 2010
Posts: 4
Ok i think the next thing i sure will do is to deeply review the forum rules to be sure next time i won't step out of it because i wasn't intended to.
Although i do understand that it is in everybody's best interest to speak & write clearly & in an understandable way, i do also think that you also need to distinguish one extreme to one another & mostly not miss the big picture here, which were for me to express that i was thankful to have had answers .... be here again thank to you too for the advice !
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39396
    
  28
The rules are there for a reason. Unindented code is difficult to read, and people who didn't grow up speaking and writing English will have difficulty understand your abbreviations.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3646
    
  15
understanding


Joanne
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Fail to access attributes from a DOM Document