Tag: XML

How to Indent XML String in Java (Pretty)

Hello Guys,

This is a cool way to prettify your XML (String format) in Java Language:

public static String format(String xml, Boolean ommitXmlDeclaration) throws IOException, SAXException, ParserConfigurationException {

DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = db.parse(new InputSource(new StringReader(xml)));

OutputFormat format = new OutputFormat(doc);
format.setIndenting(true);
format.setIndent(2);
format.setOmitXMLDeclaration(ommitXmlDeclaration);
format.setLineWidth(Integer.MAX_VALUE);
Writer outxml = new StringWriter();
XMLSerializer serializer = new XMLSerializer(outxml, format);
serializer.serialize(doc);

return outxml.toString();

}
Advertisements

[Python] – XML Validator against XML Schema (XSD)

Hi,

This is the code:

from easyxsd import *
import sys

# Load XML Schema (.xsd file)
xsd = xsd_from_file(sys.argv[1])

# Load XML File
xml = xml_from_file(sys.argv[2])

# Validate
list_errors = validate_with_errors(xml, xsd)

# Open output file (validation response)
f = open(sys.argv[3],'w')

for error in list_errors:
    f.write(str(error) + '\n')

f.close()

mime_exeDownload here an executable version for Windows

To execute:
validate_xml.exe C:\Documents\MyFolder\schema.xsd C:\Documents\MyFolder\myfile.xml C:\Documents\MyFolder\outputvalidate.txt

The shortcut works too:
validate_xml.exe schema.xsd myfile.xml outputvalidate.txt

Credits:
https://github.com/gnrfan/python-easyxsd