草庐IT

java - 获取 XSD 验证错误的父元素

coder 2024-06-29 原文

我有以下带有单位和度量子元素的 xml。

<Depth>
    <measure>1.00</measure>
    <unit>in</unit>
<Depth>
<Width>
    <measure>1.00</measure>
    <unit>in</unit>
</Width>
<vendorPackHeight>
    <measure>1.00</measure>
    <unit>in</unit>
</vendorPackHeight>
<Weight>
    <measure>7.00</measure>
    <unit>LBS</unit> //invalid expected value is lb
</Weight> 

当单位或度量子元素的 XSD 验证失败并出现类似 cvc-enumeration-valid 的错误(当度量不是来自一组枚举值时)或单位值因 cvc-datatype-valid.1.2.1 而失败时数据类型不匹配如何获取父元素?在上面的 xml 中,它将是重量。

在 SAXParseException 中,我得到了发生错误的行号。是否可以从行号中获取元素,然后获取其父元素?

最佳答案

我认为在 Java API 中没有执行此操作的标准方法。但是,某些库确实允许您偷看它当前在哪个元素上。例如在 Apache Xerces 实现中,它支持通过

获取当前节点

getProperty("http://apache.org/xml/properties/dom/current-element-node")

在他们的网站上查看有关该属性的文档:https://xerces.apache.org/xerces2-j/properties.html#dom.current-element-node


Xerces library默认情况下由您的 JDK 提供,但也可以作为第 3 方库导入到您的项目中。如果您必须拥有它才能正确运行您的应用程序,我建议您添加它。下面是一些示例代码,用于验证 XSD 的 XML 文档并获取当前节点。

import java.io.ByteArrayInputStream;
import java.io.IOException;

import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Source;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

import org.apache.xerces.impl.Constants;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.xml.sax.ErrorHandler;
import org.xml.sax.SAXException;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import org.xml.sax.SAXParseException;

public class XSDTest {

    public static void main(String[] args) throws SAXException, IOException, ParserConfigurationException {

        // our XSD, which defines 1 node  <TheNode> which must have decimal text content 
        byte [] schemaData = ("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"
                + "<xs:schema xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">"
                    + "<xs:simpleType name=\"theNodeType\">"
                        + "<xs:restriction base=\"xs:decimal\"/>"
                    + "</xs:simpleType>"
                    + "<xs:element name=\"TheNode\" type=\"theNodeType\"/>"
                + "</xs:schema>").getBytes();
        // our invalid xml
        byte [] xmlData = "<TheNode>123NotADecimal</TheNode>".getBytes();

        // parse schema
        SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
        Source schemaSource = new StreamSource(new ByteArrayInputStream(schemaData));
        Schema schema = schemaFactory.newSchema(schemaSource);

        // build our document, must use document builder to enable xerces parser properties for DOM
        // Also must be a xerces implementation of the DBF, should be enabled by default in a standard java project but just to be verbose about it
        // pass in the full name of the DBF impl
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(org.apache.xerces.jaxp.DocumentBuilderFactoryImpl.class.getName(), XSDTest.class.getClassLoader());
        dbf.setNamespaceAware(true);
        Document doc = dbf.newDocumentBuilder().parse(new ByteArrayInputStream(xmlData));


        // configure our validator and parse the document.
        Validator validator = schema.newValidator();
        validator.setErrorHandler(new MyErrorHandler(validator));
        validator.validate(new DOMSource(doc.getDocumentElement()));
    }

    private static class MyErrorHandler implements ErrorHandler {
        private final Validator xsdValidator;

        public MyErrorHandler(Validator xsdValidator) {
            this.xsdValidator = xsdValidator;
        }
        @Override
        public void warning(SAXParseException exception) throws SAXException {
            System.out.println("Warning on node: " + getCurrentNode());
            System.out.println(exception.getLocalizedMessage());
        }

        @Override
        public void error(SAXParseException exception) throws SAXException {
            System.out.println("Error on node: " + getCurrentNode());
            System.out.println(exception.getLocalizedMessage());
        }

        @Override
        public void fatalError(SAXParseException exception) throws SAXException {
            System.out.println("Fatal on node: " + getCurrentNode());
            System.out.println(exception.getLocalizedMessage());
        }


        private Node getCurrentNode() throws SAXNotRecognizedException, SAXNotSupportedException {
            // get prop "http://apache.org/xml/properties/dom/current-element-node"
            // see https://xerces.apache.org/xerces2-j/properties.html#dom.current-element-node
            return (Node)xsdValidator.getProperty(Constants.XERCES_PROPERTY_PREFIX + Constants.CURRENT_ELEMENT_NODE_PROPERTY);
        }
    }

}

输出:

Error on node: [TheNode: null]
cvc-datatype-valid.1.2.1: '123NotADecimal' is not a valid value for 'decimal'.
Error on node: [TheNode: null]
cvc-type.3.1.3: The value '123NotADecimal' of element 'TheNode' is not valid.

关于java - 获取 XSD 验证错误的父元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38577102/

有关java - 获取 XSD 验证错误的父元素的更多相关文章

  1. ruby-on-rails - 如何验证 update_all 是否实际在 Rails 中更新 - 2

    给定这段代码defcreate@upgrades=User.update_all(["role=?","upgraded"],:id=>params[:upgrade])redirect_toadmin_upgrades_path,:notice=>"Successfullyupgradeduser."end我如何在该操作中实际验证它们是否已保存或未重定向到适当的页面和消息? 最佳答案 在Rails3中,update_all不返回任何有意义的信息,除了已更新的记录数(这可能取决于您的DBMS是否返回该信息)。http://ar.ru

  2. ruby - 具有身份验证的私有(private) Ruby Gem 服务器 - 2

    我想安装一个带有一些身份验证的私有(private)Rubygem服务器。我希望能够使用公共(public)Ubuntu服务器托管内部gem。我读到了http://docs.rubygems.org/read/chapter/18.但是那个没有身份验证-如我所见。然后我读到了https://github.com/cwninja/geminabox.但是当我使用基本身份验证(他们在他们的Wiki中有)时,它会提示从我的服务器获取源。所以。如何制作带有身份验证的私有(private)Rubygem服务器?这是不可能的吗?谢谢。编辑:Geminabox问题。我尝试“捆绑”以安装新的gem..

  3. ruby-on-rails - Rails 常用字符串(用于通知和错误信息等) - 2

    大约一年前,我决定确保每个包含非唯一文本的Flash通知都将从模块中的方法中获取文本。我这样做的最初原因是为了避免一遍又一遍地输入相同的字符串。如果我想更改措辞,我可以在一个地方轻松完成,而且一遍又一遍地重复同一件事而出现拼写错误的可能性也会降低。我最终得到的是这样的:moduleMessagesdefformat_error_messages(errors)errors.map{|attribute,message|"Error:#{attribute.to_s.titleize}#{message}."}enddeferror_message_could_not_find(obje

  4. ruby-on-rails - 如果为空或不验证数值,则使属性默认为 0 - 2

    我希望我的UserPrice模型的属性在它们为空或不验证数值时默认为0。这些属性是tax_rate、shipping_cost和price。classCreateUserPrices8,:scale=>2t.decimal:tax_rate,:precision=>8,:scale=>2t.decimal:shipping_cost,:precision=>8,:scale=>2endendend起初,我将所有3列的:default=>0放在表格中,但我不想要这样,因为它已经填充了字段,我想使用占位符。这是我的UserPrice模型:classUserPrice回答before_val

  5. ruby-on-rails - 如何验证非模型(甚至非对象)字段 - 2

    我有一个表单,其中有很多字段取自数组(而不是模型或对象)。我如何验证这些字段的存在?solve_problem_pathdo|f|%>... 最佳答案 创建一个简单的类来包装请求参数并使用ActiveModel::Validations。#definedsomewhere,atthesimplest:require'ostruct'classSolvetrue#youcouldevencheckthesolutionwithavalidatorvalidatedoerrors.add(:base,"WRONG!!!")unlesss

  6. java - 等价于 Java 中的 Ruby Hash - 2

    我真的很习惯使用Ruby编写以下代码:my_hash={}my_hash['test']=1Java中对应的数据结构是什么? 最佳答案 HashMapmap=newHashMap();map.put("test",1);我假设? 关于java-等价于Java中的RubyHash,我们在StackOverflow上找到一个类似的问题: https://stackoverflow.com/questions/22737685/

  7. ruby-on-rails - 如何将验证与模型分开 - 2

    我有一些非常大的模型,我必须将它们迁移到最新版本的Rails。这些模型有相当多的验证(User有大约50个验证)。是否可以将所有这些验证移动到另一个文件中?说app/models/validations/user_validations.rb。如果可以,有人可以提供示例吗? 最佳答案 您可以为此使用关注点:#app/models/validations/user_validations.rbrequire'active_support/concern'moduleUserValidationsextendActiveSupport:

  8. ruby-on-rails - 跳过状态机方法的所有验证 - 2

    当我的预订模型通过rake任务在状态机上转换时,我试图找出如何跳过对ActiveRecord对象的特定实例的验证。我想在reservation.close时跳过所有验证!叫做。希望调用reservation.close!(:validate=>false)之类的东西。仅供引用,我们正在使用https://github.com/pluginaweek/state_machine用于状态机。这是我的预订模型的示例。classReservation["requested","negotiating","approved"])}state_machine:initial=>'requested

  9. ruby-on-rails - 迷你测试错误 : "NameError: uninitialized constant" - 2

    我遵循MichaelHartl的“RubyonRails教程:学习Web开发”,并创建了检查用户名和电子邮件长度有效性的测试(名称最多50个字符,电子邮件最多255个字符)。test/helpers/application_helper_test.rb的内容是:require'test_helper'classApplicationHelperTest在运行bundleexecraketest时,所有测试都通过了,但我看到以下消息在最后被标记为错误:ERROR["test_full_title_helper",ApplicationHelperTest,1.820016791]test

  10. ruby - 如何在 Rails 4 中使用表单对象之前的验证回调? - 2

    我有一个服务模型/表及其注册表。在表单中,我几乎拥有服务的所有字段,但我想在验证服务对象之前自动设置其中一些值。示例:--服务Controller#创建Action:defcreate@service=Service.new@service_form=ServiceFormObject.new(@service)@service_form.validate(params[:service_form_object])and@service_form.saverespond_with(@service_form,location:admin_services_path)end在验证@ser

随机推荐