Solved

Dynamic XML Validation with JAVA

Posted on 2006-10-25
8
347 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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 

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

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

Question has a verified solution.

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

Suggested Solutions

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
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.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will the learn the benefit of plain text editors and code an HTML5 based template for use in further tutorials.

821 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