Question

can anybody tell me please, how can i set the value of attribute in sax element so, that it'll be looks like, that id scales up :<document> <el id="1"><text>Motivationsschreiben.</text></el> <el id="2"><text>Sehr geehrte Damen und Herren.</text></el></document>.

I try it out so:

public class Element1 {

Element e = null;
BufferedReader in;
StreamResult out;

TransformerHandler th;
AttributesImpl atts;

public static void main(String[] args) {

    new Element1().doit();
}

public void doit() {
    try {
        in = new BufferedReader(new FileReader("D:\\Probe.txt"));
        out = new StreamResult("D:\\data.xml");
        initXML();
        String str;
        while ((str = in.readLine()) != null) {
            process(str);
        }
        in.close();
        closeXML();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public void initXML() throws ParserConfigurationException,
        TransformerConfigurationException, SAXException {
    // JAXP + SAX
    SAXTransformerFactory tf = (SAXTransformerFactory) SAXTransformerFactory
            .newInstance();

    th = tf.newTransformerHandler();
    Transformer serializer = th.getTransformer();
    serializer.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
    // pretty XML output
    serializer.setOutputProperty(
            "{http://xml.apache.org/xslt}indent-amount", "4");
    serializer.setOutputProperty(OutputKeys.INDENT, "yes");
    th.setResult(out);
    th.startDocument();
    atts = new AttributesImpl();

    th.startElement("", "", "document", atts);
}

public void process(String s) throws SAXException {

    try {

        String[] elements = s.split("\\|");
        atts.clear();
        int i = 0;
        i++;

        atts.addAttribute("", "", "Id", "", "" + i);

        th.startElement("", "", "el", atts);
        th.startElement("", "", "text", atts);
        th.characters(elements[0].toCharArray(), 0, elements[0].length());
        th.endElement("", "", "text");
        th.endElement("", "", "el");

    }

    catch (Exception e) {
        System.out.print("Out of bounds! DOH! should have used vectors");
    }
}

public void closeXML() throws SAXException {
    th.endElement("", "", "document");
    th.endDocument();
}

}

and as a result i become this:<?xml version="1.0" encoding="ISO-8859-1"?><document> <el Id="1"> <text Id="1">Motivationsschreiben</text> </el> <el Id="1"> <text Id="1">Sehr geehrte Damen und Herren</text> </el> <el Id="1"> <text Id="1">Mein Name </text> </el>

but i want that id goes up, like this: <?xml version="1.0" encoding="ISO-8859-1"?><document> <el Id="1"> <text Id="1">Motivationsschreiben</text> </el> <el Id="2"> <text Id="2">Sehr geehrte Damen und Herren</text> </el> <el Id="3"> <text Id="3">Mein Name </text> </el>

Input file just simple text file

Motivationsschreiben Sehr geehrte Damen und Herren Mein Name

Can you give me some advise, what's wrong here?

Thanks a lot

Was it helpful?

Solution

You variable i which is used to generate the id value is defined in your method. It will then be recreated every time the method is called. Define it as a class variable:

class Element1 {
 ...
 int i = 0;
 ...
public void process(String s) throws SAXException {

    try {

        String[] elements = s.split("\\|");
        atts.clear();
        i++;

        atts.addAttribute("", "", "Id", "", "" + i);

        th.startElement("", "", "el", atts);
        th.startElement("", "", "text", atts);
        th.characters(elements[0].toCharArray(), 0, elements[0].length());
        th.endElement("", "", "text");
        th.endElement("", "", "el");

    }

    catch (Exception e) {
        System.out.print("Out of bounds! DOH! should have used vectors");
    }
}}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top