• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Fail to access attributes from a DOM Document

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 !
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.

 
miu san
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 !
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please UseCodeTags next time.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Duelly noted thkx for the advice!
 
Campbell Ritchie
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please don't write "thnx" or similar.
 
miu san
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
understanding
 
reply
    Bookmark Topic Watch Topic
  • New Topic