Naimisha
Naimisha

Reputation: 21

XML validation in Java - extra closing tag >

I've been trying to write junit test cases that figure check the syntax of an XML file, make sure all tags are closed, etc.

I am currently Using a dom parser in the test, as it throws an exception on invalid XML but it isn't working when I have an extra closing tag for my XML eg: if i have

 private static final String OM_APPLICATION_STRG = "<abc>**>**\r\n" + 
            "</abc>\r\n" + 
            ""; 

instead of

private static final String OM_APPLICATION_STRG = "<abc>\r\n" + 
        "</abc>\r\n" + 
        "";    

Please let me know if there is any other way to test xml validity including extra closing tag

Upvotes: 1

Views: 1299

Answers (3)

A4L
A4L

Reputation: 17595

<abc>**>**</abc>

is valid XML.

What you need to do is define a schema for XML and set restrictions on the content of text nodes.

A simple example would be schema.xsd

<?xml version="1.0"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
    <xs:element name="abc">
        <xs:simpleType>
            <xs:restriction base="xs:string">
                <xs:pattern value="[\sa-zA-Z0-9\*]*" />
            </xs:restriction>
        </xs:simpleType>
    </xs:element>
</xs:schema>

The regular expression

[\sa-zA-Z0-9\*]*

defines the set of character allowed in the text node inside of the element <abc>

The following document would validate

<?xml version="1.0"?>
<abc xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:noNamespaceSchemaLocation="schema.xsd">
    hello ** world
</abc>

If you add a > charcter

hello ** > ** world

the XML will no longer validate against the schema.

For validating against a schema see RomanticNerd's answer.

Upvotes: 0

Romantic Nerd
Romantic Nerd

Reputation: 21

May be there is another way —— SchemaFactory

You can define a .xsd file for your xml string, then use SchemaFactory to validate your xml string by the .xsd file. Like this example

   String xml = xmlFile.toString();
   try {
      URL schema = Resources.getResource("/XXX.xsd");
      Validator validator = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema").newSchema(schema)
            .newValidator();
      Source source = new StreamSource(new CharArrayReader(xml.toCharArray()));
      validator.validate(source);
   } catch (Exception e) {
      // the xml is not valid for your .xsd defination
   }

Upvotes: 2

Erwin Bolwidt
Erwin Bolwidt

Reputation: 31269

An "extra closing tag" is not a concept that exists. It is just a "greater-than" symbol in a text node in an XML file, and it's perfectly valid to have them. You don't need to check for them in your validator.

For example, this is completely valid xml:

<?xml version="1.0"?>

<xml>></xml>

Upvotes: 3

Related Questions