当前位置:   article > 正文

Java_XmlSchema_Validate_eodprice

eodprice

xml file

<EODPriceSnapshots xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://api.company.com/">
    <MessageInfo>
        <MessageCode>200</MessageCode>
        <MessageDetail>Request data successfully</MessageDetail>
    </MessageInfo>
    <GeneralInfo>
        <ShareClassId>0P000xxxx</ShareClassId>
        <CompanyName>International Business Machines Corp</CompanyName>
        <ExchangeId>xxx</ExchangeId>
        <Symbol>yyy</Symbol>
        <CUSIP>888888</CUSIP>
        <CIK>11111</CIK>
        <ISIN>US3333</ISIN>
        <SEDOL>2005973</SEDOL>
        <CountryId>uuu</CountryId>
    </GeneralInfo>
    <EODPriceSnapshotEntity>
        <TradingDate>2016-09-07</TradingDate>
        <OpenPrice>160.19</OpenPrice>
        <HighPrice>161.76</HighPrice>
        <LowPrice>160</LowPrice>
        <ClosePrice>161.64</ClosePrice>
        <Volume>2866237</Volume>
        <PriceCurrencyId>xxx</PriceCurrencyId>
        <PreviousTradingDate>2016-09-07</PreviousTradingDate>
        <PreviousClosePrice>160.35</PreviousClosePrice>
    </EODPriceSnapshotEntity>
</EODPriceSnapshots>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28

xsd file:

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" attributeFormDefault="unqualified" elementFormDefault="qualified" targetNamespace="http://api.company.com/">
    <xs:element name="EODPriceSnapshots">
        <xs:complexType>
            <xs:sequence>
                <xs:element name="MessageInfo">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element type="xs:short" name="MessageCode"/>
                            <xs:element type="xs:string" name="MessageDetail"/>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
                <xs:element name="GeneralInfo">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element type="xs:string" name="ShareClassId"/>
                            <xs:element type="xs:string" name="CompanyName"/>
                            <xs:element type="xs:string" name="ExchangeId"/>
                            <xs:element type="xs:string" name="Symbol"/>
                            <xs:element type="xs:int" name="CUSIP"/>
                            <xs:element type="xs:int" name="CIK"/>
                            <xs:element type="xs:string" name="ISIN"/>
                            <xs:element type="xs:int" name="SEDOL"/>
                            <xs:element type="xs:string" name="CountryId"/>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
                <xs:element name="EODPriceSnapshotEntity">
                    <xs:complexType>
                        <xs:sequence>
                            <xs:element type="xs:date" name="TradingDate"/>
                            <xs:element type="xs:float" name="OpenPrice"/>
                            <xs:element type="xs:float" name="HighPrice"/>
                            <xs:element type="xs:short" name="LowPrice"/>
                            <xs:element type="xs:float" name="ClosePrice"/>
                            <xs:element type="xs:int" name="Volume"/>
                            <xs:element type="xs:string" name="PriceCurrencyId"/>
                            <xs:element type="xs:date" name="PreviousTradingDate"/>
                            <xs:element type="xs:float" name="PreviousClosePrice"/>
                        </xs:sequence>
                    </xs:complexType>
                </xs:element>
            </xs:sequence>
        </xs:complexType>
    </xs:element>
</xs:schema>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46

Validate the xml schema

package CommonTool;  
import java.io.ByteArrayInputStream;  
import java.io.File;  
import java.io.InputStream;  

import javax.xml.transform.Source;  
import javax.xml.transform.stream.StreamSource;  
import javax.xml.validation.Schema;  
import javax.xml.validation.SchemaFactory;  
import javax.xml.validation.Validator;  

import org.apache.log4j.Logger;  
import org.xml.sax.SAXException;  

/** 
 * @desc    xml验证 
 * @author  copy 
 * @date    2016-09-08
 * 
 */  
public class XmlValidateUtil {  

    private static final Logger logger = Logger.getLogger(XmlValidateUtil.class);  

    private static final String SCHEMALANG = "http://www.w3.org/2001/XMLSchema";  

    /** 
     * Schema校验xml文件 
     * @param xmlPath xml字符串 
     * @param xsdPath xsd文件路径 
     * @return xml文件是否符合xsd定义的规则 
     */  
    public static boolean xmlStringValidate(String xmlStr, String xsdPath) {  
        boolean flag = false;  
        try {  
            SchemaFactory factory = SchemaFactory.newInstance(SCHEMALANG);  
            File schemaLocation = new File(xsdPath);  
            Schema schema = factory.newSchema(schemaLocation);  
            Validator validator = schema.newValidator();  
            InputStream is = new ByteArrayInputStream(xmlStr.getBytes());  
            Source source = new StreamSource(is);  
            try {  
                validator.validate(source);  
                flag = true;  
            } catch (SAXException ex) {  
                logger.info(ex.getMessage());  
            }  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return flag;  
    }  

    /** 
     * Schema校验xml文件 
     * @param xmlPath xml文件路径 
     * @param xsdPath xsd文件路径 
     * @return xml文件是否符合xsd定义的规则 
     */  
    public static boolean xmlFileValidate(String xmlPath, String xsdPath) {  
        boolean flag = false;  
        try {  
            SchemaFactory factory = SchemaFactory.newInstance(SCHEMALANG);  
            File schemaLocation = new File(xsdPath);  
            Schema schema = factory.newSchema(schemaLocation);  
            Validator validator = schema.newValidator();  
            Source source = new StreamSource(xmlPath);  

            try {  
                validator.validate(source);  
                flag = true;  
            } catch (SAXException ex) {  
                logger.info(ex.getMessage());  
            }  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return flag;  
    }  


    public static void main(String[] args) {
        String xmlPath = "D:\\workspace\\groovyStudy\\data\\xmlExample.xml";
        String xsdPath = "D:\\workspace\\groovyStudy\\data\\xsdExample.xsd";
        System.out.println("Test result is: " + xmlFileValidate(xmlPath, xsdPath));
    }
}  
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家小花儿/article/detail/286840
推荐阅读
相关标签
  

闽ICP备14008679号