JAXP-DOM验证XML

None.gif import java.io.File;
None.gifimport java.io.IOException;
None.gifimport java.io.OutputStreamWriter;
None.gifimport java.io.Writer;
None.gif
None.gif
//  JAXP
None.gif
import javax.xml.XMLConstants;
None.gifimport javax.xml.parsers.FactoryConfigurationError;
None.gifimport javax.xml.parsers.ParserConfigurationException;
None.gifimport javax.xml.parsers.DocumentBuilderFactory;
None.gifimport javax.xml.parsers.DocumentBuilder;
None.gifimport javax.xml.transform.Source;
None.gifimport javax.xml.transform.dom.DOMSource;
None.gifimport javax.xml.transform.stream.StreamSource;
None.gifimport javax.xml.validation.Schema;
None.gifimport javax.xml.validation.SchemaFactory;
None.gifimport javax.xml.validation.Validator;
None.gif
None.gif
//  DOM
None.gif
import org.w3c.dom.Document;
None.gifimport org.w3c.dom.DocumentType;
None.gifimport org.w3c.dom.NamedNodeMap;
None.gifimport org.w3c.dom.Node;
None.gifimport org.w3c.dom.NodeList;
None.gif
ExpandedBlockStart.gifContractedBlock.gif
public   class  TestDOMValidation  dot.gif {
InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif    
public static void main(String[] args) dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif        
try dot.gif{
ExpandedSubBlockStart.gifContractedSubBlock.gif            
if (args.length != 2dot.gif{
InBlock.gif                System.err.println (
"Usage: java TestDOMValidation " +
InBlock.gif                                    
"[xml filename] [schema filename]");
InBlock.gif                System.exit (
1);
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
// Get Document Builder Factory
InBlock.gif
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
InBlock.gif
InBlock.gif            
// Leave off validation, and turn off namespaces
InBlock.gif
            factory.setValidating(false);
InBlock.gif            factory.setNamespaceAware(
false);
InBlock.gif
InBlock.gif            DocumentBuilder builder 
= factory.newDocumentBuilder();
InBlock.gif            Document doc 
= builder.parse(new File(args[0]));
InBlock.gif
InBlock.gif            
// Handle validation
InBlock.gif
            SchemaFactory constraintFactory = 
InBlock.gif                SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
InBlock.gif            Source constraints 
= new StreamSource(new File(args[1]));
InBlock.gif            Schema schema 
= constraintFactory.newSchema(constraints);
InBlock.gif            Validator validator 
= schema.newValidator();
InBlock.gif
InBlock.gif            
// Validate the DOM tree
ExpandedSubBlockStart.gifContractedSubBlock.gif
            try dot.gif{
InBlock.gif                validator.validate(
new DOMSource(doc));
InBlock.gif                System.
out.println("Document validates fine.");
ExpandedSubBlockStart.gifContractedSubBlock.gif            }
 catch (org.xml.sax.SAXException e) dot.gif{
InBlock.gif                System.
out.println("Validation error: " + e.getMessage());
ExpandedSubBlockEnd.gif            }

InBlock.gif
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 catch (ParserConfigurationException e) dot.gif{
InBlock.gif            System.
out.println("The underlying parser does not support the requested features.");
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 catch (FactoryConfigurationError e) dot.gif{
InBlock.gif            System.
out.println("Error occurred obtaining Document Builder Factory.");
ExpandedSubBlockStart.gifContractedSubBlock.gif        }
 catch (Exception e) dot.gif{
InBlock.gif            e.printStackTrace();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值