2

I try to update XML file which was read from the database, saved as a XMLType and then i performed SAXParse saving in variables all information i needed to use to construct further queries to the database. Basing on the values I've read I'm checking some conditions and then I want to update values of 3 nodes. How can I update the values. Below is the code I use to parse document but I have no idea how to update XML file in java using SAX.

public void parseXML(int i) throws XMLParseException, SAXException, IOException, SQLException {
    String xml = printXML(i);
    saxParser.parse(new InputSource(new StringReader(xml)), handler);
}

And in handler i have various conditions to save things I'm interested in like:

public class UserHandler extends DefaultHandler {

    StringBuilder builder = new StringBuilder();
    private Data data = new Data();
    boolean idOrder = false; 
    boolean idReader = false; 


   @Override
   public void startElement(String uri, 
   String localName, String qName, Attributes attributes)
      throws SAXException {
      if (qName.equalsIgnoreCase("order")) {
         data.setIdOrder(attributes.getValue("ID_ORDER"));
      } else if (qName.equalsIgnoreCase("id_reader")) {
         idReader = true;
      } 
      builder.setLength(0);
   }

   @Override
   public void endElement(String uri, 
   String localName, String qName) throws SAXException {
      if (qName.equalsIgnoreCase("id_reader")) {
          data.setIdReader(builder.toString());
      } 
   }

   @Override
   public void characters(char ch[], 
      int start, int length) throws SAXException {

       if (idReader) {
           builder.append(new String(ch, start, length));
          } 
   }
}

Please give me some hints.

monterinio
  • 49
  • 2
  • 11

0 Answers0