sách gpt4 ai đã đi

Bộ phân tích cú pháp SAX không tuân theo các tham chiếu

In lại 作者:行者123 更新时间:2023-12-04 15:13:57 31 4
mua khóa gpt4 Nike

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

我正在尝试调整示例代码以从此处解析 XML https://gist.github.com/helderdarocha/8791651 .我不明白为什么 SAX 解析器不遵循引用。

这是我的 xsd 定义的两个示例。

第一个文件有如下定义



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">















第二个文件的标题如下



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 的大量段定义。下面是一个例子

   





























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

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 currentSequence;

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

@Ghi đè
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);
} khác {
rootElement = currentElement;
}
}

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

@Ghi đè
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;
}
}

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

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

private void printTree(SchemaElement element, String indent) {
System.out.println(indent + element.getName() + " : " + element.getType());
Map attributes = element.getAttributes();
if (attributes != null) {
for (Map.Entry entry : attributes.entrySet()) {
System.out.println(" @" + entry.getKey() + " : " + simpleTypes.get(entry.getValue()));
}
}
List 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 children;
private Map 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 getChildren() {
return children;
}
public void setChildren(List children) {
this.children = children;
}
public Map getAttributes() {
return attributes;
}
public void setAttributes(Map attributes) {
this.attributes = attributes;
}


}

class SchemaComplexType {
private String name;
private String reference;
private List children;
private Map attributes = new HashMap<>();
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List getChildren() {
return children;
}
public void setChildren(List children) {
this.children = children;
}
public Map getAttributes() {
return attributes;
}
public void setAttributes(Map 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);
}
}

有什么想法吗?感谢您的帮助。

Cảm ơn.

1 Câu trả lời

听起来这里有两个不同的概念在起作用。

如果验证 SAX 解析器被用来解析一段 XML,并根据它的模式验证它:

<>
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">

...等等,然后很明显,当该模式在幕后解析时,解析器将需要遵循其中的任何引用和导入。

但是,如果 .xsd 本身就是被解析的 XML,那么正如您已经发现的那样,它的元素将直接传递到 ContentHandler 中。上面的 SchemaSaxHandler 需要做一些更多的工作来学习每个 xs:element - 就像你已经在为 simpleTypescomplexTypes 映射 - 所以它们以后可以从 tham khảo 中解析。

如果您需要的是 XML 模式中已解析元素和类型的模型,那么值得探索幕后模式模型 - 在 XML 解析器(如 Xerces)中。作为起点,这是使用 XNI - Xerces native 接口(interface):

    File baseDir = new File("/myschemas");

XMLEntityResolver entityResolver = new XMLEntityResolver() {

@Ghi đè
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

关于xsd - SAX 解析器不遵循引用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49563112/

31 4 0
行者123
Hồ sơ cá nhân

Tôi là một lập trình viên xuất sắc, rất giỏi!

Nhận phiếu giảm giá Didi Taxi miễn phí
Mã giảm giá Didi Taxi
Giấy chứng nhận ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com