Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add attributes to a XML node using StAX?

Tags:

java

xml

stax

I need to generate a node in a XML file with the following structure:

<node attribute0="value0" attribute1="value1" > </node>

How can I do it in StAX?

Edit 1: I'm trying the code from Section "3.4. Write XML File- Example" from Lars Vogel's tutorial (http://www.vogella.de/articles/JavaXML/article.html)

like image 847
gtludwig Avatar asked Mar 03 '26 13:03

gtludwig


2 Answers

given the link you added it appears you use teh below syntax. have a look at his advanced tutorial for writting RSS feed here

StartElement rssStart = eventFactory.createStartElement("", "", "rss");
eventWriter.add(rssStart);
eventWriter.add(eventFactory.createAttribute("version", "2.0"));
eventWriter.add(end);
like image 76
olly_uk Avatar answered Mar 06 '26 02:03

olly_uk


If you would use XMLStreamWriter instead of the XMLEventWriter, you can do it the following way:

xmlStreamWriter.writeStartElement("node");
xmlStreamWriter.writeAttribute("attribute0","value0");
xmlStreamWriter.writeAttribute("attribute1","value1");
xmlStreamWriter.writeEndElement();

But also for XMLEventWriter, there is a Method to create Attributes:

xmlEventWriter.createAttribute(name, value);

Regards, Max

like image 45
Max Avatar answered Mar 06 '26 02:03

Max