Java 如何获取属性';XML/XSD中的s元素名称
这是我用来解析的XMLJava 如何获取属性';XML/XSD中的s元素名称,java,scala,xsd,xml-parsing,Java,Scala,Xsd,Xml Parsing,这是我用来解析的XML <bookstore> <book category="children"> <title>Harry Potter</title> <author>J K. Rowling</author> <year>2005</year> <price>29.99</price> </book>
<bookstore>
<book category="children">
<title>Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="web">
<title>Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
对于这个特定的示例,您可以使用以下XPath
/*[local-name()='attribute'和@name='category']/祖先::*[local-name()='element'][1]/@name
下面是一个在Java中使用它的示例:
public static void main(String[] args) throws Exception {
DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = db.parse(new InputSource(new StringReader(xml)));
XPathExpression xpath = XPathFactory.newInstance().newXPath().compile(".//*[local-name()='attribute' and @name='category']/ancestor::*[local-name()='element'][1]/@name");
String name = xpath.evaluate(doc);
System.out.println(name);
}
private static String xml = "<xs:schema attributeFormDefault=\"unqualified\" elementFormDefault=\"qualified\" xmlns:xs=\"http://www.w3.org/2001/XMLSchema\">\n"
+ " <xs:element name=\"bookstore\">\n"
+ " <xs:complexType>\n"
+ " <xs:sequence>\n"
+ " <xs:element name=\"book\" maxOccurs=\"unbounded\" minOccurs=\"0\">\n"
+ " <xs:complexType>\n"
+ " <xs:sequence>\n"
+ " <xs:element type=\"xs:string\" name=\"title\"/>\n"
+ " <xs:element type=\"xs:string\" name=\"author\"/>\n"
+ " <xs:element type=\"xs:short\" name=\"year\"/>\n"
+ " <xs:element type=\"xs:float\" name=\"price\"/>\n"
+ " </xs:sequence>\n"
+ " <xs:attribute type=\"xs:string\" name=\"category\" use=\"optional\"/>\n"
+ " </xs:complexType>\n"
+ " </xs:element>\n"
+ " </xs:sequence>\n"
+ " </xs:complexType>\n"
+ " </xs:element>\n"
+ "</xs:schema>";
publicstaticvoidmain(字符串[]args)引发异常{
DocumentBuilder db=DocumentBuilderFactory.newInstance().newDocumentBuilder();
documentdoc=db.parse(新的InputSource(新的StringReader(xml));
XPathExpression xpath=XPathFactory.newInstance().newXPath().compile(“./*[local-name()='attribute'和@name='category']/concenter::*[local-name()='element'][1]/@name”);
字符串名称=xpath.evaluate(doc);
System.out.println(名称);
}
私有静态字符串xml=“\n”
+“\n”
+“\n”
+“\n”
+“\n”
+“\n”
+“\n”
+“\n”
+“\n”
+“\n”
+“\n”
+“\n”
+“\n”
+“\n”
+“\n”
+“\n”
+“\n”
+“\n”
+ "";
您可以使用@
访问属性。例如,假设我们有:
val bookStoreXml =
<bookstore>
<book category="children">
<title>Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="web">
<title>Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
将提供:
List(children, web)
代码在上运行。您可以在上阅读Alvin Alexander提供的有关高级xml解析的更多信息。感谢Alexandra,我能够获取属性类别的元素名称。感谢Tomer的回复,我在解析XSD和提取值时接受了下面的答案。
val bookStoreXml =
<bookstore>
<book category="children">
<title>Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="web">
<title>Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
<price>39.95</price>
</book>
</bookstore>
(bookStoreXml \ "book").map(_ \ "@category")
List(children, web)