Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ionic-framework/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Xsd SAX解析器不';不要跟随参考资料_Xsd_Sax_Saxparser - Fatal编程技术网

Xsd SAX解析器不';不要跟随参考资料

Xsd SAX解析器不';不要跟随参考资料,xsd,sax,saxparser,Xsd,Sax,Saxparser,我试图从xsd解析HL7消息定义。我将模式定义分为两个文件。第一个文件包含实际的消息定义,第二个文件包含消息中的段定义 我试图调整一个示例代码,从这里解析XML。我不明白为什么SAX解析器不遵循引用 下面是我的xsd定义的两个示例 第一个文件具有以下定义 <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:x

我试图从xsd解析HL7消息定义。我将模式定义分为两个文件。第一个文件包含实际的消息定义,第二个文件包含消息中的段定义

我试图调整一个示例代码,从这里解析XML。我不明白为什么SAX解析器不遵循引用

下面是我的xsd定义的两个示例

第一个文件具有以下定义

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
           targetNamespace="http://www.xsd_porcessor.org/parser" 
           xmlns="http://www.xsd_porcessor.org/parser" 
           elementFormDefault="qualified"
           attributeFormDefault="unqualified">

           <xs:include schemaLocation="segments.xsd"/>

           <xs:complexType name="ADT.01.MESSAGE">
                <xs:sequence>
                    <xs:element maxOccurs="1" minOccurs="1" ref="MSH"/>
                    <xs:element maxOccurs="1" minOccurs="1" ref="EVN"/>
                    <xs:element maxOccurs="1" minOccurs="1" ref="PID"/>
                    <xs:element maxOccurs="1" minOccurs="1" ref="PV1"/>
                    <xs:element maxOccurs="1" minOccurs="1" ref="IN1"/>
                    <xs:element maxOccurs="1" minOccurs="1" ref="IN2"/>
                </xs:sequence>
           </xs:complexType>
           <xs:element name="ADT.A01" type="ADT.01.MESSAGE"/>
</xs:schema>

第二个文件具有以下标题

<?xml version="1.1" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" 
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
           targetNamespace="http://www.xsd_porcessor.org/parser" 
           xmlns="http://www.xsd_porcessor.org/parser" 
           elementFormDefault="qualified"
           attributeFormDefault="unqualified">

…以及表示为complexTypes的多段定义。贝娄就是一个例子

   <xs:complexType name="MSH.SEGMENT">
        <xs:sequence>
            <xs:element maxOccurs="1" minOccurs="1" ref="MSH.1.FieldSeparator"/>
            <xs:element maxOccurs="1" minOccurs="1" ref="MSH.2.ServiceString"/>
            <xs:element maxOccurs="1" minOccurs="1" ref="MSH.3.SendingApplication"/>
            <xs:element maxOccurs="1" minOccurs="0" ref="MSH.4.SendingFacility"/>
            <xs:element maxOccurs="1" minOccurs="0" ref="MSH.5.ReceivingApplication"/>
            <xs:element maxOccurs="1" minOccurs="0" ref="MSH.6.ReceivingFacility"/>
            <xs:element maxOccurs="1" minOccurs="1" ref="MSH.7.DateTimeOfMessage"/>
            <xs:element maxOccurs="1" minOccurs="0" ref="MSH.8.Security"/>
            <xs:element maxOccurs="1" minOccurs="1" ref="MSH.9.MessageType"/>
            <xs:element maxOccurs="1" minOccurs="1" ref="MSH.10.MessageControlID"/>
            <xs:element maxOccurs="1" minOccurs="1" ref="MSH.11.ProcessingID"/>
            <xs:element maxOccurs="1" minOccurs="1" ref="MSH.12.VersionID"/>
            <xs:element maxOccurs="1" minOccurs="0" ref="MSH.13.SequenceNumber"/>
            <xs:element maxOccurs="1" minOccurs="0" ref="MSH.14.ContinuationPointer"/>
            <xs:element maxOccurs="1" minOccurs="0" ref="MSH.15.AcceptAcknowledgmentType"/>
            <xs:element maxOccurs="1" minOccurs="0" ref="MSH.16.ApplicationAcknowledgmentType"/>
            <xs:element maxOccurs="1" minOccurs="0" ref="MSH.17.CountryCode"/>
            <xs:element maxOccurs="unbounded" minOccurs="0" ref="MSH.18.CharacterSet"/>
            <xs:element maxOccurs="1" minOccurs="0" ref="MSH.19.PrincipalLanguageOfMessage"/>
            <xs:element maxOccurs="1" minOccurs="0" ref="MSH.20.AlternateCharacterSetHandlingScheme"/>
            <xs:element maxOccurs="unbounded" minOccurs="0" ref="MSH.21.MessageProfileIdentifier"/>
            <xs:element maxOccurs="1" minOccurs="0" ref="MSH.22.SendingResponsibleOrganization"/>
            <xs:element maxOccurs="1" minOccurs="0" ref="MSH.23.ReceivingResponsibleOrganization"/>
            <xs:element maxOccurs="1" minOccurs="0" ref="MSH.24.SendingNetworkAddress"/>
            <xs:element maxOccurs="1" minOccurs="0" ref="MSH.25.ReceivingNetworkAddress"/>
        </xs:sequence>
    </xs:complexType>  
    <xs:element name="MSH" type="MSH.SEGMENT"/>

这里是一个经过调整的解析器本身

package ca.parser.xml;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.parsers.*;

import org.xml.sax.*;

import org.xml.sax.helpers.*;

public class SAXReaderExample {

    public static final String PATH = "resources";

    public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException {
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader reader = sp.getXMLReader();
        reader.setContentHandler(new SchemaSaxHandler());
        reader.parse(new InputSource(new FileInputStream(new File(PATH, "messages.xsd"))));
    }
}


class SchemaSaxHandler extends DefaultHandler {

    // temporary - always null when tag closes
    private String currentSimpleTypeName;
    private String currentSimpleTypeBaseType;
    private SchemaElement currentElement;
    private SchemaComplexType currentComplexType;
    private List<SchemaElement> currentSequence;

    // cumulative - will use the data when XML finishes
    private Map<String, String> simpleTypes = new HashMap<>();
    private Map<String, SchemaComplexType> complexTypes = new HashMap<>();
    private SchemaElement rootElement;

    @Override
    public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {

        if (qName.equals("xs:simpleType")) {
            currentSimpleTypeName = atts.getValue("name");
        }
        if (qName.equals("xs:restriction")) {
            currentSimpleTypeBaseType = atts.getValue("base");
        }

        if (qName.equals("xs:complexType")) {
            currentComplexType = new SchemaComplexType();

            currentComplexType.setName(atts.getValue("name"));
        }

        if (qName.equals("xs:sequence")) {
            currentSequence = new ArrayList<>();
        }

        if (qName.equals("xs:element")) {
            currentElement = new SchemaElement();
            if (atts.getValue("name")==null) {
                currentElement.setName(atts.getValue("ref"));
            }else {
                currentElement.setName(atts.getValue("name"));
            }
            currentElement.setType(atts.getValue("type"));
            currentElement.setReference(atts.getValue("ref"));
             if (currentSequence != null) {
                currentSequence.add(currentElement);
            } else {
                rootElement = currentElement;
            }
        }

        if (qName.equals("xs:attribute")) {
            currentComplexType.addAttribute(atts.getValue("name"), atts.getValue("type"));
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName) throws SAXException {
        if (qName.equals("xs:simpleType")) {
            simpleTypes.put(currentSimpleTypeName, currentSimpleTypeBaseType);
            currentSimpleTypeName = null;
            currentSimpleTypeBaseType = null;
        }
        if (qName.equals("xs:complexType")) {
            complexTypes.put(currentComplexType.getName(), currentComplexType);
            currentComplexType = null;
        }
        if (qName.equals("xs:sequence")) {
            if (currentComplexType != null) {
                currentComplexType.setChildren(currentSequence);
            }
            currentSequence = null;
        }
    }

    @Override
    public void endDocument() throws SAXException {
        makeTree(rootElement);
        printTree(rootElement, "");
    }

    public void makeTree(SchemaElement element) {
        SchemaComplexType type = complexTypes.get(element.getType());
        if (type != null) {
            List<SchemaElement> children = type.getChildren();
            element.setChildren(children);
            for (SchemaElement child : children) {
                makeTree(child);
            }
            element.setAttributes(type.getAttributes());
        } else {
            element.setType(simpleTypes.get(element.getType()));
        }
    }

    private void printTree(SchemaElement element, String indent) {
        System.out.println(indent + element.getName() + " : " + element.getType());
        Map<String, String> attributes = element.getAttributes();
        if (attributes != null) {
            for (Map.Entry<String, String> entry : attributes.entrySet()) {
                System.out.println("    @" + entry.getKey() + " : " + simpleTypes.get(entry.getValue()));
            }
        }
        List<SchemaElement> children = element.getChildren();
        if (children != null) {
            for (SchemaElement child : children) {
                printTree(child, indent + "    ");
            }
        }
    }
    class SchemaElement {

        private String name;
        private String type;
        private String reference;
        public String getReference() {
            return reference;
        }
        public void setReference(String reference) {
            this.reference = reference;
        }
        private List<SchemaElement> children;
        private Map<String, String> attributes;
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public String getType() {
            return type;
        }
        public void setType(String type) {
            this.type = type;
        }
        public List<SchemaElement> getChildren() {
            return children;
        }
        public void setChildren(List<SchemaElement> children) {
            this.children = children;
        }
        public Map<String, String> getAttributes() {
            return attributes;
        }
        public void setAttributes(Map<String, String> attributes) {
            this.attributes = attributes;
        }


    }

    class SchemaComplexType {        
        private String name;
        private String reference;
        private List<SchemaElement> children;
        private Map<String, String> attributes = new HashMap<>();
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public List<SchemaElement> getChildren() {
            return children;
        }
        public void setChildren(List<SchemaElement> children) {
            this.children = children;
        }
        public Map<String, String> getAttributes() {
            return attributes;
        }
        public void setAttributes(Map<String, String> attributes) {
            this.attributes = attributes;
        }

        public String getReference() {
            return reference;
        }

        public void setReference(String reference) {
            this.reference=reference;
        }

        public void addAttribute(String name,String type) {
            attributes.put(name, type);
        }
    }
包ca.parser.xml;
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.IOException;
导入java.util.ArrayList;
导入java.util.HashMap;
导入java.util.List;
导入java.util.Map;
导入javax.xml.parsers.*;
导入org.xml.sax.*;
导入org.xml.sax.helpers.*;
公共类SAXReaderExample{
公共静态最终字符串PATH=“resources”;
公共静态void main(字符串[]args)抛出ParserConfiguration异常、SAXException、IOException{
SAXParserFactory spf=SAXParserFactory.newInstance();
SAXParser sp=spf.newSAXParser();
XMLReader=sp.getXMLReader();
setContentHandler(新SchemaSaxHandler());
parse(新的InputSource(新的FileInputStream(新的文件(路径,“messages.xsd”))));
}
}
类SchemaSaxHandler扩展了DefaultHandler{
//临时-标记关闭时始终为空
私有字符串currentSimpleTypeName;
私有字符串currentSimpleTypeBaseType;
私有SchemaElement当前元素;
私有SchemaComplexType currentComplexType;
私有列表序列;
//累积-将在XML完成时使用数据
私有映射simpleType=newHashMap();
私有映射complexTypes=new HashMap();
私有SchemaElement根元素;
@凌驾
public void startElement(字符串uri、字符串localName、字符串qName、属性atts)引发异常{
if(qName.equals(“xs:simpleType”)){
currentSimpleTypeName=atts.getValue(“名称”);
}
if(qName.equals(“xs:restriction”)){
currentSimpleTypeBaseType=atts.getValue(“基”);
}
if(qName.equals(“xs:complexType”)){
currentComplexType=新模式ComplexType();
currentComplexType.setName(atts.getValue(“名称”);
}
if(qName.equals(“xs:sequence”)){
currentSequence=新的ArrayList();
}
if(qName.equals(“xs:element”)){
currentElement=新的SchemaElement();
if(atts.getValue(“name”)==null){
currentElement.setName(atts.getValue(“ref”);
}否则{
currentElement.setName(atts.getValue(“名称”);
}
currentElement.setType(atts.getValue(“类型”);
currentElement.setReference(atts.getValue(“ref”);
if(currentSequence!=null){
currentSequence.add(currentElement);
}否则{
rootElement=currentElement;
}
}
if(qName.equals(“xs:attribute”)){
currentComplexType.addAttribute(atts.getValue(“名称”)、atts.getValue(“类型”);
}
}
@凌驾
public void endElement(字符串uri、字符串localName、字符串qName)引发SAXException{
if(qName.equals(“xs:simpleType”)){
put(currentSimpleTypeName,currentSimpleTypeBaseType);
currentSimpleTypeName=null;
currentSimpleTypeBaseType=null;
}
if(qName.equals(“xs:complexType”)){
complexTypes.put(currentComplexType.getName(),currentComplexType);
currentComplexType=null;
}
if(qName.equals(“xs:sequence”)){
如果(currentComplexType!=null){
currentComplexType.setChildren(currentSequence);
}
currentSequence=null;
}
}
@凌驾
public void endDocument()引发异常{
makeTree(rootElement);
printTree(根元素“”);
}
公共void生成树(SchemaElement元素){
SchemaComplexType类型=complexTypes.get(element.getType());
if(type!=null){
List children=type.getChildren();
元素。setChildren(children);
for(SchemaElement子项:子项){
makeTree(儿童);
}
setAttributes(type.getAttributes());
}否则{
setType(simpleType.get(element.getType());
}
}
私有void打印树(SchemaElement元素,字符串缩进){
System.out.println(indent+element.getName()+“:”+element.getType());
映射属性=element.getAttributes();
如果(属性!=null){
for(Map.Entry:attributes.entrySet()){
System.out.println(“@”+entry.getKey()+”:“+simpleType.get(entry.getValue());
}
}
List children=element.getChildren();
如果(子项!=null){
for(SchemaElement子项:子项){
printTree(子级,缩进+“”);
}
}
}
类模式元素{
<xmlRootElement
    xmlns="http://www.xsd_porcessor.org/parser"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.xsd_porcessor.org/parser messages.xsd">
    File baseDir = new File("/myschemas");

    XMLEntityResolver entityResolver = new XMLEntityResolver() {

        @Override
        public XMLInputSource resolveEntity(
                XMLResourceIdentifier resourceIdentifier)
                throws XNIException, IOException {
            // E.g. resourceIdentifier.getLiteralSystemId() will be segments.xsd
            String uri = new File(baseDir,
                    resourceIdentifier.getLiteralSystemId()).toURI()
                    .toString();
            return new XMLInputSource(null, uri, null);
        }
    };
    XMLSchemaLoader loader = new XMLSchemaLoader();
    loader.setEntityResolver(entityResolver);

    XSModel model = loader
            .loadURI(new File(baseDir, "messages.xsd").toURI()
                    .toString());

    System.out.println(model.getComponents(XSConstants.ELEMENT_DECLARATION));
{http://www.xsd_porcessor.org/parser}ADT.A01="http://www.xsd_porcessor.org/parser":ADT.A01