xml file operation
<p>I need to generate an ID for the tags which have the content. I am using STAX for reading the XML. I have read the file and got the tags which have content but I can't generate their ID properly. Below I have posted the java code.</p>

<p>xml:</p>

<pre class="lang-xml prettyprint-override"><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
&lt;?xml-stylesheet type="text/xsl" href="TuneTestgram.xsl"?&gt;
&lt;root&gt;
    &lt;titlegroup&gt;
        &lt;titlelabel id="desc1"&gt;Unit 1&lt;/titlelabel&gt;
        &lt;maintitle id="desc2"&gt;&lt;/maintitle&gt;
        &lt;hr/&gt;
    &lt;/titlegroup&gt;
    &lt;header&gt;
        &lt;div&gt;  
            &lt;title id="desc3"&gt;Main points dad&lt;/title&gt;
            &lt;P id="desc4"&gt;svn&lt;br/&gt;&lt;/P&gt;
            &lt;P id="desc5"&gt;This is a book published by harper collins on 
                          &lt;em&gt;&lt;u&gt;XML .&lt;/u&gt;&lt;/em&gt;&lt;br/&gt;&lt;br/&gt;This is a book that has&lt;br/&gt;
            &lt;/P&gt;
            &lt;P id="desc6"&gt;Clauses can also have another noun group as the object or 
                          complement. test&lt;br/&gt;line1&lt;br/&gt;line1.1&lt;br/&gt;line1.2&lt;br/&gt;
                          line2&lt;br/&gt;line3&lt;br/&gt;&lt;br/&gt;&lt;br/&gt;
            &lt;/P&gt;
            &lt;P id="desc7"&gt;Clauses can &lt;span style="text-decoration: underline;"&gt;
                          have&lt;/span&gt; an adverbial, 
                          &lt;sup&gt;&lt;span style="font-style: italic;"&gt;also&lt;/span&gt;&lt;/sup&gt; 
                          called &lt;sub&gt;an&lt;/sub&gt; adjunct.
                          &lt;span style="font-weight: bold;"&gt;ram&lt;/span&gt;
            &lt;/P&gt;
            &lt;P id="desc8"&gt;Changing &lt;br/&gt;&lt;/P&gt;
            &lt;P id="desc9"&gt;Compound sentences consist of two or more main clauses. 
                          Complex sentences always include a subordinate clause, 
                          as well as one or more main clauses.dad dad dgah test
            &lt;/P&gt;
        &lt;/div&gt;
    &lt;/header&gt;
    &lt;list&gt;
        &lt;label id="desc10"&gt;1&lt;/label&gt;
        &lt;li id="desc11"&gt;A simple sentence has one clause, beginning with a noun group 
                        called the subject. The subject is the person or thing that 
                        the sentence is about. This is followed by a verb group, which 
                        tells you what the subject is doing, or describes the 
                        subject's situation.dad adhagf hfhgfhgf das
        &lt;/li&gt;
    &lt;/list&gt;
    &lt;P id="desc12"&gt;&lt;i&gt;Iwaited.&lt;strong&gt;dad&lt;/strong&gt;&lt;/i&gt;&lt;/P&gt; 
    &lt;P id="desc13"&gt;&lt;i&gt;Thegirlscreamed.&lt;/i&gt;&lt;strong&gt;dad&lt;/strong&gt;&lt;/P&gt;
&lt;/root&gt;
</code></pre>

<hr>

<pre><code>package pack;

import java.io.FileReader;
import java.io.FileWriter;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLOutputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamReader;
import javax.xml.stream.XMLStreamWriter;

public class testingstax 
{

    public static void main(String args[])throws Exception
    {
        FileReader input= new FileReader("E:\\Java\\teststax\\src\\pack\\TuneTest.xml");
        XMLInputFactory factory = XMLInputFactory.newInstance();
        XMLStreamReader r = factory.createXMLStreamReader(input);
        FileWriter fw=new FileWriter("E:\\Java\\teststax\\src\\pack\\TuneTest2.xml");
        XMLOutputFactory output = XMLOutputFactory.newInstance();
        XMLStreamWriter writer = output.createXMLStreamWriter(fw );
        String s3;
        int i,n,j=0;
        try 
        {
            int event = r.getEventType()
            while (true) 
            {
                switch (event) 
                {
                    case XMLStreamConstants.START_DOCUMENT:
                        System.out.println("Start Document.");
                        break;
                    case XMLStreamConstants.START_ELEMENT:
                        System.out.println("Start Element: " + r.getName());
                        String s=r.getName().toString();
                        writer.writeStartElement(s);

                        for( i = 0, n = r.getAttributeCount(); i &lt; n; ++i)
                            writer.writeAttribute(r.getAttributeName(i).toString(),
                                                  r.getAttributevalue(i));
                        break;
                    case XMLStreamConstants.CHARACTERS:
                        if (r.isWhiteSpace())
                            break;   
                        System.out.println("Text: " + r.getText());
                        writer.writeAttribute("ids",String.valueOf(++j));
                        writer.writeCharacters(r.getText());
                        break;
                    case XMLStreamConstants.END_ELEMENT:
                        System.out.println("End Element:" + r.getName());
                        writer.writeEndElement();
                        break;
                    case XMLStreamConstants.END_DOCUMENT:
                        System.out.println("End Document.");
                        break;
                }

                if (!r.hasNext())
                    break;
                event = r.next();
                // writer.writeCharacters("\n");
            }
        } finally 
        {
            writer.flush();
            writer.close();
            r.close();
        }
    }
}
</code></pre>

<p>exception:</p>

<pre><code>Exception in thread "main" javax.xml.stream.XMLStreamException: Attribute not associated with any element
    at com.sun.xml.internal.stream.writers.XMLStreamWriterImpl.writeAttribute(Unknown Source)
    at pack.testingstax.main(testingstax.java:55)
</code></pre>
