Parsing and updating xml using SAX parser in java

webExplorer

I have an xml file with similar tags ->

<properties>
   <definition>
      <name>IP</name>
      <description></description>
      <defaultValue>10.1.1.1</defaultValue>
   </definition>
   <definition>
      <name>Name</name>
      <description></description>
      <defaultValue>MyName</defaultValue>
   </definition>
   <definition>
      <name>Environment</name>
      <description></description>
      <defaultValue>Production</defaultValue>
   </definition>
</properties>

I want to update the default value of the definition with name : Environment.

Is it possible to do that using SAX parser?

Can you please point me to proper documentation?

So far I have parsed the document but when I update defaultValue, it updates all defaultValues. I dont know how to parse the exact default value tag.

stringy05

Anything is possible with SAX, it's just waaaaay harder than it has to be. It's pretty old school and there are many easier ways to do this (JAXB, XQuery, XPath, DOM etc ).

That said lets do it with SAX.

It sounds like the problem you are having is that you are not tracking the state of your progress through the document. SAX simply works by making the callbacks when it stumbles across an event within the document

This is a fairly crude way of parsing the doc and updating the relevant node using SAX. Basically I am checking when we hit a element with the value you want to update (Environment) and setting a flag so that when we get to the contents of the defaultValue node, the characters callback lets me remove the existing value and replace it with the new value.

import java.io.StringReader;
import java.util.Arrays;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

public class Q26897496 extends DefaultHandler {

    public static String xmlDoc = "<?xml version='1.0'?>"   
            + "<properties>"
            + "   <definition>" 
            + "      <name>IP</name>"  
            + "      <description></description>"
            + "      <defaultValue>10.1.1.1</defaultValue>"
            + "   </definition>" 
            + "   <definition>"
            + "      <name>Name</name>" 
            + "      <description></description>"
            + "      <defaultValue>MyName</defaultValue>" 
            + "   </definition>"
            + "   <definition>" 
            + "      <name>Environment</name>"
            + "      <description></description>"
            + "      <defaultValue>Production</defaultValue>"
            + "   </definition>" 
            + "</properties>";


    String elementName;
    boolean mark = false;
    char[] updatedDoc;

    public static void main(String[] args) {

        Q26897496 q = new Q26897496();
        try {
            q.parse();
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    public Q26897496() {

    }

    public void parse() throws Exception {
        SAXParserFactory spf = SAXParserFactory.newInstance();
        spf.setNamespaceAware(true);
        SAXParser saxParser = spf.newSAXParser();
        XMLReader xml = saxParser.getXMLReader();
        xml.setContentHandler(this);
        xml.parse(new InputSource(new StringReader(xmlDoc)));

        System.out.println("new xml: \n" + new String(updatedDoc));
    }

    @Override
    public void startDocument() throws SAXException {
        System.out.println("starting");

    }

    @Override
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
        this.elementName = localName;
    }

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

        String value = new String(ch).substring(start, start + length);
        if (elementName.equals("name")) {
            if (value.equals("Environment")) {
                this.mark = true;
            }
        }

        if (elementName.equals("defaultValue") && mark == true) {
            // update
            String tmpDoc = new String(ch);
            String leading = tmpDoc.substring(0, start);
            String trailing = tmpDoc.substring(start + length, tmpDoc.length());
            this.updatedDoc = (leading + "NewValueForDefaulValue" + trailing).toCharArray();
            mark = false;
        }
    }
}

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Parsing XML using SAX Parser in Python 3

From Dev

Parsing XML using SAX Parser in Python 3

From Dev

Java : Parsing xml file using SAX/XPATH

From Dev

Android XML using SAX Parser

From Dev

Parsing an XML file with Nokogiri SAX parser

From Dev

How to Parse XML without using DOM or SAX Parser in Java?

From Dev

parsing XML in Java using SAX: value cut in 2 halves

From Dev

parsing XML in Java using SAX: value cut in 2 halves

From Dev

How to skip the parent tag, based on the value of a child tag in XML Parsing using SAX Parser

From Dev

Exception for no memory while parsing large XML file in SAX parser

From Dev

SLOW SPEED in using SAX Parser to parse XML data and save it to mysql localhost (JAVA)

From Dev

Parsing issue in SAX parser android

From Dev

Java sax parser bug

From Dev

How to retrieve value of XML element using a Nokogiri SAX Parser?

From Dev

Parse XML file containing umlaute using SAX parser

From Dev

Unable to Parse Parent / Child Elements using SAX XML Parser (Android)

From Dev

traversing xml document using SAX parser and printing output in desired format

From Dev

Difficulty in parsing xml using DOM Parser

From Dev

Parsing an html document using an XML-parser

From Dev

Java SAX is not parsing properly

From Dev

SAX Parsing in Java

From Dev

Read parent and child at the same time using SAX Parser in Java

From Dev

Android SAX XML parsing error

From Dev

Perl XML::SAX partial parsing

From Dev

how to find the node which has child node while parsing XML file using DOM parser in java

From Dev

how to find the node which has child node while parsing XML file using DOM parser in java

From Dev

Handle external Entities and Stylesheet in Sax Parser (XML)

From Dev

execute bundle who cintains xml parser (SAX)

From Dev

Why SAX xml parser taking long time

Related Related

  1. 1

    Parsing XML using SAX Parser in Python 3

  2. 2

    Parsing XML using SAX Parser in Python 3

  3. 3

    Java : Parsing xml file using SAX/XPATH

  4. 4

    Android XML using SAX Parser

  5. 5

    Parsing an XML file with Nokogiri SAX parser

  6. 6

    How to Parse XML without using DOM or SAX Parser in Java?

  7. 7

    parsing XML in Java using SAX: value cut in 2 halves

  8. 8

    parsing XML in Java using SAX: value cut in 2 halves

  9. 9

    How to skip the parent tag, based on the value of a child tag in XML Parsing using SAX Parser

  10. 10

    Exception for no memory while parsing large XML file in SAX parser

  11. 11

    SLOW SPEED in using SAX Parser to parse XML data and save it to mysql localhost (JAVA)

  12. 12

    Parsing issue in SAX parser android

  13. 13

    Java sax parser bug

  14. 14

    How to retrieve value of XML element using a Nokogiri SAX Parser?

  15. 15

    Parse XML file containing umlaute using SAX parser

  16. 16

    Unable to Parse Parent / Child Elements using SAX XML Parser (Android)

  17. 17

    traversing xml document using SAX parser and printing output in desired format

  18. 18

    Difficulty in parsing xml using DOM Parser

  19. 19

    Parsing an html document using an XML-parser

  20. 20

    Java SAX is not parsing properly

  21. 21

    SAX Parsing in Java

  22. 22

    Read parent and child at the same time using SAX Parser in Java

  23. 23

    Android SAX XML parsing error

  24. 24

    Perl XML::SAX partial parsing

  25. 25

    how to find the node which has child node while parsing XML file using DOM parser in java

  26. 26

    how to find the node which has child node while parsing XML file using DOM parser in java

  27. 27

    Handle external Entities and Stylesheet in Sax Parser (XML)

  28. 28

    execute bundle who cintains xml parser (SAX)

  29. 29

    Why SAX xml parser taking long time

HotTag

Archive