Forums Register Login

Fail to access attributes from a DOM Document

+Pie Number of slices to send: Send
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 !
+Pie Number of slices to send: Send
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.

+Pie Number of slices to send: Send
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 !
+Pie Number of slices to send: Send
Please UseCodeTags next time.
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
Duelly noted thkx for the advice!
+Pie Number of slices to send: Send
+Pie Number of slices to send: Send
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 !
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
understanding
Paper beats rock. Scissors beats tiny ad.
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 1093 times.
Similar Threads
Reading UTF-8 encoded Data in JSP using DOM
Converting an XML document to HTML using Javascript and HTML
charset and pageEncoding in jsp
Page section does not update after call of a4j:support in IE 9
Getting the arraylist through session
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 04:18:22.