设置SAX元素的属性

时间:2013-11-27 18:44:14

标签: java xml sax

任何人都可以告诉我,我怎么能在sax元素中设置属性的值,这样看起来,id会扩大:<document> <el id="1"><text>Motivationsschreiben.</text></el> <el id="2"><text>Sehr geehrte Damen und Herren.</text></el></document>

我试试看:

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();
}

}

因此我变成了这个:<?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>

但我希望ID上升,如下所示:<?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>

输入文件只是简单的文本文件

Motivationsschreiben Sehr geehrte Damen和Herren 我的名字

你能给我一些建议,这里有什么不对吗?

非常感谢

1 个答案:

答案 0 :(得分:1)

用于生成id值的变量i在您的方法中定义。每次调用该方法时都会重新创建它。将其定义为类变量:

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");
    }
}}