我正在尝试使用XSD解析Apache XMLSchema Core。我能够解析该文件并将Parent及其Child Element信息存储在HashMap中。
每个Child元素都存储在XmlSchemaElement类型中,该类型还包含该子元素的Parent信息。当我试图查看与子元素相关的信息时,它会显示所有元素的Root元素,而不是它的直接父元素。
例如:在我的XSD中,RestaurantMenu是Root元素,它的直接子元素是Food。而且,Food有子元素name、price、calories等等。当我看到name的父元素时,我希望它是Food,因为它是直接的父元素,但是在Debug显示RestaurantMenu作为它的父元素时,这有点让人困惑。
我想知道如何获得与每个元素的直接父元素相关的信息。
以下是我的XSD文件:
<xs:schema attributeFormDefault="unqualified"
elementFormDefault="qualified"
xmlns:xs="http://www.w3.org/2001/XMLSchema">
<xs:element name="RestaurantMenu">
<xs:complexType>
<xs:sequence>
<xs:element name="food" maxOccurs="5" minOccurs="0">
<xs:complexType>
<xs:sequence>
<xs:element type="xs:string" name="name" />
<xs:element type="xs:string" name="price" />
<xs:element type="xs:string" name="description" />
<xs:element type="xs:short" name="calories" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:schema>下面是我使用Apache XmlSchema Core XML Parser编写的代码:我正在尝试使用Debug:
List<XmlSchemaElement> childElements = getElementType(new QName("food"));
System.out.println(childElements.get(0));以上代码将返回食物的所有Children元素:name、price、calories、description、ingredients。然后,我试图看到其中一个元素的父元素,即name。下面是我要选择的调试路径:
childElements -> [0] -> namedDelegate -> parentSchema -> items -> [0] -> namedDelegate -> qName.在这个qName中,我期望food是我的直系亲属,但是我得到了RestaurantMenu,这是我的Root xsd元素。
请查找以下完整代码:
public class TestParser {
private static XmlSchemaCollection xmlSchemaCollection;
private static Map<QName, List<XmlSchemaElement>> xsdElements = new HashMap<QName, List<XmlSchemaElement>>();
private static List<XmlSchemaElement> schemaElements = new ArrayList<XmlSchemaElement>();
public static void main(String[] args) throws URISyntaxException, FileNotFoundException, UnsupportedEncodingException {
// Path for the file which is stored within the Resource folder
String xsdPath = Paths.get(TestParser.class.getClassLoader().getResource("test.xsd").toURI()).toFile().getAbsolutePath();
String filePath = Path.of(xsdPath).toString();
InputStream is = new FileInputStream(filePath);
xmlSchemaCollection = new XmlSchemaCollection();
// Schema contain the complete XSD content which needs to be parsed
XmlSchema schema = xmlSchemaCollection.read(new StreamSource(is));
// schema.write(System.out);
// Get the root element from XSD
Map.Entry<QName, XmlSchemaElement> entry = schema.getElements().entrySet().iterator().next();
// Get all the elements based on the parent element
XmlSchemaElement childElement = xmlSchemaCollection.getElementByQName(entry.getKey());
// Call method to get all the child elements
getChildElementNames(childElement);
// Get the elements type based on choice
List<XmlSchemaElement> childElements = xsdElements.get(new QName("food"));
System.out.println(childElements.get(0));
}
// Method to check for the child elements and return list
private static void getChildElementNames(XmlSchemaElement element) {
// Get the type of the element
XmlSchemaType elementType = element != null ? element.getSchemaType() : null;
// Confirm if the element is of Complex type
if (elementType instanceof XmlSchemaComplexType) {
// Get all particles associated with that element Type
XmlSchemaParticle allParticles = ((XmlSchemaComplexType) elementType).getParticle();
// Check particle belongs to which type
if (allParticles instanceof XmlSchemaSequence) {
// System.out.println("Sequence Schema Type");
final XmlSchemaSequence xmlSchemaSequence = (XmlSchemaSequence) allParticles;
final List<XmlSchemaSequenceMember> items = xmlSchemaSequence.getItems();
items.forEach((item) -> {
XmlSchemaElement itemElements = (XmlSchemaElement) item;
schemaElements.add(itemElements);
addChild(element.getQName(), itemElements);
// Call method recursively to get all subsequent element
getChildElementNames(itemElements);
schemaElements = new ArrayList<XmlSchemaElement>();
});
}
}
}
// Add child elements based on its parent
private static void addChild(QName qName, XmlSchemaElement child) {
List<XmlSchemaElement> values = xsdElements.get(qName);
if (values == null) {
values = new ArrayList<XmlSchemaElement>();
}
values.add(child);
xsdElements.put(qName, values);
}
}发布于 2021-04-09 09:28:42
在使用了一段时间使用Java的Apache XMLSchema Core库之后,下面是我的观察。
没有办法直接使用库获取直接父元素,您可以从XmlSchemaElement获取与当前元素及其子元素相关的信息。
发布于 2021-04-01 18:40:02
免责声明:我不是XSModel方面的专家,但我曾经使用过它,很久以前。
XSModel是XSD中几乎所有内容的完整和完整记录。因此,它确实包含您正在寻找的信息。这可能有助于认识到XSModel是为验证XML文档而创建的--它从未打算成为XSD模型的通用内存表示形式。如果这正是您所需要的,那么您可能会更好地使用Eclipse模型。无论哪种方式,加载和操作XSD模型都是一项少数运动,您不应该期望API会被完美地记录下来。
请您协助我如何在Apache XMLSchema核心库中为子元素找到一个直接父元素。
当您谈论“子元素”时,我认为您指的是local element declaration或element reference。通常,这两种类型都可以有多个父元素声明,因为它们可以在全局复杂类型中声明。如果多个元素声明使用了全局复杂类型定义,那么您的“子元素”将有多个可能的父元素。
https://stackoverflow.com/questions/66901116
复制相似问题