XML Validator

XML Validator

Use our XML validator to syntax-check your XML.

XML Errors Will Stop You

Errors in XML documents will stop your XML applications.

The W3C XML specification states that a program should stop processing an XML document if it finds an error. The reason is that XML software should be small, fast, and compatible.

HTML browsers will display documents with errors (like missing end tags). HTML browsers are big and incompatible because they have a lot of unnecessary code to deal with (and display) HTML errors.

With XML, errors are not allowed.


Syntax-Check Your XML

To help you syntax-check your XML, we have created an XML validator.

Paste your XML into the text area below, and syntax-check it by clicking the "Validate" button.

 

Note: This only checks if your XML is "Well formed". If you want to validate your XML against a DTD, see the last paragraph on this page.


Syntax-Check an XML File

You can syntax-check an XML file by typing the URL of the file into the input field below, and then click the "Validate" button:

Filename:
 

Note: If you get an "Access denied" error, it's because your browser security does not allow file access across domains.

The file "note_error.xml" demonstrates your browsers error handling. If you want see an error free message, substitute the "note_error.xml" with "cd_catalog.xml".


Validate Your XML Against a DTD

If you know DTD, you can validate your XML in the text area below.

Just add the DOCTYPE declaration to your XML and click the "Validate" button:

 

Note: Only Internet Explorer will actually check your XML against the DTD. Firefox, Mozilla, Netscape, and Opera will not.

Java 中可以使用一些库来实现 JSON 和 XML 文件的验证,比如: 1. JSON 验证器:可以使用 json-schema-validator 库来验证 JSON 文件是否符合指定的 JSON Schema。以下是一个简单的示例: ```java import com.github.fge.jsonschema.core.exceptions.ProcessingException; import com.github.fge.jsonschema.main.JsonSchema; import com.github.fge.jsonschema.main.JsonSchemaFactory; import com.github.fge.jsonschema.core.report.ProcessingReport; import com.github.fge.jsonschema.core.report.ProcessingMessage; import com.fasterxml.jackson.databind.JsonNode; import com.fasterxml.jackson.databind.ObjectMapper; import java.io.IOException; import java.net.URL; public class JsonValidator { public static void main(String[] args) throws IOException, ProcessingException { // 读取 JSON 文件和 JSON Schema 文件 String jsonData = "{ \"name\": \"John\", \"age\": 30 }"; String schemaData = "{ \"type\": \"object\", \"properties\": { \"name\": { \"type\": \"string\" }, \"age\": { \"type\": \"integer\" } } }"; ObjectMapper mapper = new ObjectMapper(); JsonNode json = mapper.readTree(jsonData); JsonNode schema = mapper.readTree(schemaData); // 创建 JSON Schema 验证器 JsonSchemaFactory factory = JsonSchemaFactory.byDefault(); JsonSchema jsonSchema = factory.getJsonSchema(schema); // 验证 JSON 文件是否符合 JSON Schema ProcessingReport report = jsonSchema.validate(json); // 打印验证结果 if (report.isSuccess()) { System.out.println("JSON 文件符合 JSON Schema!"); } else { for (ProcessingMessage message : report) { System.out.println(message.getMessage()); } } } } ``` 2. XML 验证器:可以使用 javax.xml.validation 包中提供的 API 来验证 XML 文件是否符合指定的 XSD Schema。以下是一个简单的示例: ```java import java.io.File; import javax.xml.XMLConstants; import javax.xml.validation.Schema; import javax.xml.validation.SchemaFactory; import javax.xml.validation.Validator; import org.xml.sax.SAXException; public class XmlValidator { public static void main(String[] args) { // 读取 XML 文件和 XSD Schema 文件 File xmlFile = new File("example.xml"); File xsdFile = new File("example.xsd"); try { // 创建 XML 验证器 SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); Schema schema = factory.newSchema(xsdFile); Validator validator = schema.newValidator(); // 验证 XML 文件是否符合 XSD Schema validator.validate(new StreamSource(xmlFile)); // 打印验证结果 System.out.println("XML 文件符合 XSD Schema!"); } catch (SAXException e) { System.out.println("XML 文件不符合 XSD Schema:" + e.getMessage()); } catch (IOException e) { System.out.println("无法读取 XML 文件或 XSD Schema 文件:" + e.getMessage()); } } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值