Solved

Dynamic XML Validation with JAVA

Posted on 2006-10-25
8
346 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
  • 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
 

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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
The viewer will learn how to count occurrences of each item in an array.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

911 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now