Skip to Content.
Sympa Menu

xom-interest - [XOM-interest] XMLLint for Java

xom-interest AT lists.ibiblio.org

Subject: XOM API for Processing XML with Java

List archive

Chronological Thread  
  • From: Luca Passani <passani AT eunet.no>
  • To: xom-interest <xom-interest AT lists.ibiblio.org>
  • Subject: [XOM-interest] XMLLint for Java
  • Date: Mon, 25 Sep 2006 13:20:13 +0200

Is there such thing as XMLLint for Java?

http://xmlsoft.org/xmllint.html

I see that there's a C-sharp version around (attached), so getting a Java XOM-based counterpart shouldn't be too hard...

Luca

//
// xmlint : xml validation tool.
//
// Chris Lovett, September 2001
//
using System;
using System.IO;
using System.Xml;
using System.Xml.Schema;
using System.Collections;

namespace xmlint
{
class XmlValidator
{
[STAThread]
static void Main(string[] args)
{
ArrayList files = new ArrayList();
ValidationType validation = ValidationType.Auto;

if (args.Length == 0)
{
PrintUsage();
return ;
}
XmlSchemaCollection sc = new XmlSchemaCollection();
for (int i = 0; i < args.Length; i++)
{
String arg = args[i];
if (arg[0] == '-' || arg[0] == '/') {
switch (arg[1]) {
case 'w':
validation = ValidationType.None;
break;
case 's':
char[] ch = new char[1];
ch[0] = ',';
string[] sa = arg.Substring(3).Split(ch);
Console.WriteLine("Adding:"+sa[0]+","+sa[1]);
sc.Add(sa[0], sa[1]);
validation = ValidationType.Schema;
break;
case 't':
string vt = arg.Substring(3);
try {
validation =
(ValidationType)Enum.Parse(typeof(ValidationType), vt, true);
} catch (Exception) {
Console.WriteLine("Invalid validation type '{0}',
expecting a valid System.Xml.ValidationType value", vt);
}
break;
case '?':
PrintUsage();
return;
}
}
else {
string path = Path.GetDirectoryName(args[i]);
if (path == "") path = Directory.GetCurrentDirectory();
DirectoryInfo di = new DirectoryInfo(path);
foreach (FileInfo fi in
di.GetFiles(Path.GetFileName(args[i]))) {
files.Add(fi.FullName);
}
}
}

XmlValidator v = new XmlValidator();
foreach (string filename in files) {
Console.Write(filename);
v.Check(filename, validation, sc);
}
}

bool firstError;
void Check(String filename, ValidationType validation,
XmlSchemaCollection sc)
{
firstError = true;
try
{
XmlReader reader = new XmlTextReader(filename);
if (validation != ValidationType.None)
{
XmlValidatingReader vreader = new
XmlValidatingReader(reader);
vreader.ValidationType = validation;
vreader.ValidationEventHandler += new
ValidationEventHandler(this.ValidationCallback);
if (sc != null)
{
vreader.Schemas.Add(sc);
}
reader = vreader;
}
while (reader.Read())
{
// it validates while we read !
}
Console.WriteLine(", ok");
}
catch (XmlSchemaException e)
{
ReportParseError(e);
}
catch (Exception e)
{
if (firstError) Console.WriteLine(", failed");
firstError = false;
Console.WriteLine(" ### Exception:" + e.Message);
}
}

public void ReportParseError(XmlSchemaException e)
{
if (firstError) Console.WriteLine(", failed");
firstError = false;
Console.WriteLine(" Error: " + e.Message);
if (e.SourceUri != null)
{
Console.Write(" File:");
Console.WriteLine(e.SourceUri );
}
if (e.LineNumber > 0)
{
Console.Write(" Line:");
Console.Write(e.LineNumber);
Console.Write(", Pos:");
Console.WriteLine(e.LinePosition);
}
}

public void ValidationCallback(object sender, ValidationEventArgs
args)
{
ReportParseError(args.Exception );
}

public static void PrintUsage()
{
Console.WriteLine("Usage: xmlint [-w] [-s:nsuri,xsdfile]
[-t:type] <filename>");
Console.WriteLine("Reports whether the given xml file is valid.");
Console.WriteLine("Options:");
Console.WriteLine("The -w option makes it just report
well-formedness");
Console.WriteLine("The -t specifies the schema type to use:");
Console.WriteLine(" Types are: Auto, DTD, Schema, XDR (the
default is Auto)");
Console.WriteLine("The -s option specifies a schema associated
with the given namespace");
}

}
}


  • [XOM-interest] XMLLint for Java, Luca Passani, 09/25/2006

Archive powered by MHonArc 2.6.24.

Top of Page