Solved

Dynamic XML Validation with JAVA

Posted on 2006-10-25
8
345 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
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.

707 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

18 Experts available now in Live!

Get 1:1 Help Now