【发布时间】:2021-06-28 06:01:39
【问题描述】:
我正在尝试使用 Apache XMLSchema Core 解析 XSD。我能够解析文件并将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);
}
}
【问题讨论】:
-
请您简化您的问题。那里的信息太多了。如有必要,减少您的 XSD 和代码,直到它们仅显示您所询问的问题。
-
@kimbert 感谢您的回复。为了更好地阅读,我减少了我的
XSD内容和代码。希望这可以帮助。请看一下,让我知道您是否可以针对此问题提出建议,因为我仍在寻找答案。 -
为什么你希望 parentSchema -> items -> [0] 包含特定元素的父元素?当然,parentSchema 代表整个模式模型,而 parentSchema->items 可能代表顶级声明(或其他东西)。建议您检查您的假设。
-
@kimbert 感谢您分析并提供您的解决方案。是的,后来我意识到了。我正在查看
parentSchema,它可能不包含该元素的直接父级。如果不包含,那么如何获取child的父元素?有没有其他方法可以为孩子获取immediate parentElement?
标签: java xml xsd xml-parsing xmlschema