Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7991377
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T13:16:23+00:00 2026-06-04T13:16:23+00:00

I am working on an android app which interfaces with a bluetooth camera. For

  • 0

I am working on an android app which interfaces with a bluetooth camera. For each clip stored on the camera we store some fields about the clip (some of which the user can change) in an XML file.

Currently this app is the only app writing this xml data to the device but in the future it is possible a desktop app or an iphone app may write data here too. I don’t want to make an assumption that another app couldn’t have additional fields as well (especially if they had a newer version of the app which added new fields this version didn’t support yet).

So what I want to prevent is a situation where we add new fields to this XML file in another application, and then the user goes to use the android app and its wipes out those other fields because it doesn’t know about them.

So lets take hypothetical example:

<data>
  <title>My Title</title>
  <date>12/24/2012</date>
  <category>Blah</category>
</data>

When read from the device this would get translated to a Clip object that looks like this
(simplified for brevity)

public class Clip {
  public String title, category;
  public Date date;
}

So I’m using SAX to parse the data and store it to a Clip.
I simply store the characters in StringBuilder and write them out when I reach the end element for title,category and date.

I realized though that when I write this data back to the device, if there were any other tags in the original document they would not get written because I only write out the fields I know about.

This makes me think that maybe SAX is the wrong option and perhaps I should use DOM or something else where I could more easily write out any other elements that existed originally.

Alternatively I was thinking maybe my Clip class contains an ArrayList of some generic XML type (maybe DOM), and in startTag I check if the element is not one of the predefined tags, and if so, until I reach the end of that tag I store the whole structure (but in what?).. Then upon writing back out I would just go through all of the additional tags and write them out to the xml file (along with the fields I know about of course)

Is this a common problem with a good known solution?

— Update 5/22/12 —

I didn’t mention that in the actual xml the root node (Actually called annotation), we use a version number which has been set to 1. What I’m going to do for the short term is require that the version number my app supports is >= what the version number is of the xml data. If the xml is a greater number I will attempt to parse for reading back but will deny any saves to the model. I’m still interested in any kind of working example though on how to do this.

BTW I thought of another solution that should be pretty easy. I figure I can use XPATH to find nodes that I know about and replace the content for those nodes when the data is updated. However I ran some benchmarks and the overhead is absurd in parsing the xml when it is parsed into memory. Just the parsing operation without even doing any lookups resulted in performance being 20 times worse than SAX.. Using xpath was between 30-50 times slower in general for parsing, which was really bad considering I parse these in a list view.
So my idea is to keep the SAX to parse the nodes to clips, but store the entirety of the XML in an variable of the Clip class (remember, this xml is short, less than 2kb). Then when I go to write the data back out I could use XPATH to replace out the nodes that I know about in the original XML.

Still interested in any other solutions though. I probably won’t accept a solution though unless it includes some code examples.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T13:16:24+00:00Added an answer on June 4, 2026 at 1:16 pm

    Here’s how you can go about it with SAX filters:

    1. When you read your document with SAX you record all the events. You record them and bubble them up further to the next level of SAX reader. You basically stack together two layers of SAX readers (with XMLFilter) – one will record and relay, and the other one is your current SAX handler that creates objects.
    2. When you’re ready to write your modifications back to disk you fire up the recorded SAX events layered with your writer that would overwrite those values/nodes you have altered.

    I spent some time with the idea and it worked. It basically came down to proper chaining of XMLFilters. Here’s how the unit test looks like, your code would do something similar:

    final SAXParserFactory factory = SAXParserFactory.newInstance();
    final SAXParser parser = factory.newSAXParser();
    
    final RecorderProxy recorder = new RecorderProxy(parser.getXMLReader());
    final ClipHolder clipHolder = new ClipHolder(recorder);
    
    clipHolder.parse(new InputSource(new StringReader(srcXml)));
    
    assertTrue(recorder.hasRecordingToReplay());
    
    final Clip clip = clipHolder.getClip();
    assertNotNull(clip);
    assertEquals(clip.title, "My Title");
    assertEquals(clip.category, "Blah!");
    assertEquals(clip.date, Clip.DATE_FORMAT.parse("12/24/2012"));
    
    clip.title = "My Title Updated";
    clip.category = "Something else";
    
    final ClipSerializer serializer = new ClipSerializer(recorder);
    serializer.setClip(clip);
    
    final TransformerFactory xsltFactory = TransformerFactory.newInstance();
    final Transformer t = xsltFactory.newTransformer();
    final StringWriter outXmlBuffer = new StringWriter();
    
    t.transform(new SAXSource(serializer, 
                new InputSource()), new StreamResult(outXmlBuffer));
    
    assertEquals(targetXml, outXmlBuffer.getBuffer().toString());
    

    The important lines are:

    • your SAX events recorder is wrapped around the SAX parser
    • your Clip parser (ClipHolder) is wrapped around the recorder
    • when the XML is parsed, recorder will record everything and your ClipHolder will only look at what it knows about
    • you then do whatever you need to do with the clip object
    • the serializer is then wrapped around the recorder (basically re-mapping it onto itself)
    • you then work with the serializer and it will take care of feeding the recorded events (delegating to the parent and registering self as a ContentHandler) overlayed with what it has to say about the clip object.

    Please find the DVR code and the Clip test over at github. I hope it helps.

    p.s. it’s not a generic solution and the whole record->replay+overlay concept is very rudimentary in the provided implementation. An illustration basically. If your XML is more complex and gets “hairy” (e.g. same element names on different levels, etc.) then the logic will need to be augmented. The concept will remain the same though.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on an android app in which I am using some Native
I am working on an android app in which I am seeing some disparity
I'm working on an android app - which requests some data from server and
I'm working on an Android app in which I would like to use multi-touch.
I have made an android app which working fine. I implemented the Login functionality
I am working on an android app that has a database in which one
I have an app which was tested thoroughly and working fine on Android Gingerbread
I have an Android app that I had working a few months ago which
i am beginner in Android app development i am working on a app which
I am working on an Android app where we enter some text in edit

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.