Solved

Dynamic XML Validation with JAVA

Posted on 2006-10-25
8
350 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 500 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
Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
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.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

726 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