Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何获取属性';XML/XSD中的s元素名称_Java_Scala_Xsd_Xml Parsing - Fatal编程技术网

Java 如何获取属性';XML/XSD中的s元素名称

Java 如何获取属性';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>

这是我用来解析的XML

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