?
Solved

Dynamic XML Validation with JAVA

Posted on 2006-10-25
8
Medium Priority
?
351 Views
Last Modified: 2013-11-23
I'm trying to figure out a way to dynamically validate an XML Dom object. For example I want to be able to load an XML file in to a Document object then manipulate the structure of that object in memory. Each time I make changes to the object by adding or removing elements I would like the object to be re-validated against the DTD to make sure it's still valid. When the structure becomes invalid I want error messages generated.

I know I can validate the DOM object when the Document object gets generated but I want to be able to keep the object valid as it's manipulated.

Any help here would be appreciated, some example code would be helpful.

Thanks,
dv  
0
Comment
Question by:vanfleet
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 4
8 Comments
 
LVL 14

Accepted Solution

by:
hoomanv earned 2000 total points
ID: 17809574
Some kind of in-memory validation
In JDK1.5 javax.xml.validation.Validator can do this
However you have to call the validate method everytime you make a change into the dom tree
0
 

Author Comment

by:vanfleet
ID: 17811475
Thanks for your quick responce hoomanv, your suggestion may work for me except I'm concerned about one thing. The documents that I'm parsing contain text entities, and as far as I can tell this Validation class works only with schema processing. I've not done a lot of work with Schemas so I'm not sure if my concern is valid, hopefully you can correcty me if I'm wrong. It's my understanding that text entities are not supported in Schema processing,

I have a schema for my document that we use during authoring but when we process the XML we are using a DTD because of the text entities. What would you recommend  hear?

Thanks Again,
dv
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 17811869
> we are using a DTD because of the text entities
AFAIK xsd supports text entities as well
http://www.w3schools.com/schema/schema_complex_mixed.asp

Look at the usage example of xml validation
http://java.sun.com/j2se/1.5.0/docs/api/javax/xml/validation/package-summary.html
0
7 Extremely Useful Linux Commands for Beginners

Just getting started with Linux? Here's a quick start guide that has 7 commands that we believe will come in handy.

 

Author Comment

by:vanfleet
ID: 17812533

> AFAIK xsd supports text entities as well
http://www.w3schools.com/schema/schema_complex_mixed.asp
I'm not seeing anything about text entities here. I'm reading that a complex type can contain elements attributes and text, but nothing about text entities. What am I missing? I understand that this question may be beyond the scope of my origional questions, but it it something I need to understand. (I would be happy to add a few more points for some more information about this).

The good news here is that it seems that I can generate a schema factory using a DTD. For example:

    SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.XML_DTD_NS_URI);

I'm going to spend some time playing wth this. Stay tuned...

Thanks for all the help,
dv
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 17812987
> I'm not seeing anything about text entities
As I'm not a XML expert so could you please tell me what do you mean by Text Entities ?
0
 

Author Comment

by:vanfleet
ID: 17813171
Sure, here's an example of an XML document with a text entity defined in the doctype then referenced once in the XML content:

<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE topicSet PUBLIC "-//UGS//DTD XPSBook//EN" "XPSBookEN.dtd" [
<!ENTITY textEntity "Text Entity">
]>
<elementRoot>
  <para>This is a para with a &textEntity;</para>
</elementRoot>
0
 
LVL 14

Expert Comment

by:hoomanv
ID: 17813298
Thanks I learned it
0
 

Author Comment

by:vanfleet
ID: 17818720
Thanks again for your help with this.
dv
0

Featured Post

Video: Liquid Web Managed WordPress Comparisons

If you run run a WordPress, you understand the potential headaches you may face when updating your plugins and themes. Do you choose to update on the fly and risk taking down your site; or do you set up a staging, keep it in sync with your live site and use that to test updates?

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Preface This is the third article about the EE Collaborative Login Project. A Better Website Login System (http://www.experts-exchange.com/A_2902.html) introduces the Login System and shows how to implement a login page. The EE Collaborative Logi…
In this post we will learn different types of Android Layout and some basics of an Android App.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses
Course of the Month10 days, 2 hours left to enroll

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question