我正在尝试解析XSD
using Apache XMLSchema Core
. 我能够解析文件并将Parent
其及其Child Element
信息存储在HashMap
.
每个Child
元素都存储在类型中,该类型XmlSchemaElement
也包含Parent
该子元素的信息。当我尝试查看与子元素相关的信息时,它会显示Root
所有元素而不是其直接父元素。
例如:在 myXSD
中,RestaurantMenu
是Root
元素,它的直接子元素是Food
。此外,Food
还有 childrens name
,price
等calories
。当我看到 parent 元素时,name
我希望它是Food
直接的 Parent 但在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);
}
}