java—如何提取xsd中特定于元素的属性

eyh26e7m  于 2021-07-03  发布在  Java
关注(0)|答案(0)|浏览(198)

这是我正在使用的xml。

<?xml version="1.0" encoding="UTF-8"?>
<employees>
    <details>
        <firstname>Dev</firstname>
        <lastname>Roger</lastname>
        <title>Engineer</title>
        <division>Computer</division>
        <building>301</building>
        <room>11</room>
    <room>22</room>
    <address id="1"> 
        <city> blr </city> 
        <pin>23 711314 </pin>
        <addr1>
            <state> ka</state>
                        <country>IN</country>    
        </addr1>

    </address>
    <address id="2">
        <city>chn</city> 
        <pin> 71131523</pin>
        <addr1>
            <state> wb</state>
                        <country>IN</country> 
        </addr1>

    </address>
    <book category="children">
      <title>Harry Potter</title>
      <author>J K. Rowling</author>
      <year>2005</year>
      <price>29.99</price>
    </book>
    </details>
<details>
        <firstname>Yuh</firstname>
        <lastname>Datta</lastname>
        <title>developer</title>
        <division>Computer</division>
        <building>303</building>
        <room>02</room>
    </details> 
    <details>
        <firstname>Rahil</firstname>
        <lastname>Khan</lastname>
        <title>Tester</title>
        <division>Computer</division>
        <building>304</building>
        <room>10</room>
    </details> 
    <details>
        <firstname>Deep</firstname>
        <lastname>Parekh</lastname>
        <title>Designer</title>
        <division>Computer</division>
        <building>305</building>
        <room>14</room>
    </details>
   <book order="online">
      <title>Learning XML</title>
      <author>Erik T. Ray</author>
      <year>2003</year>
      <price>39.95</price>
   </book>
</employees>

这是xsd

<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="employees">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="details" maxOccurs="unbounded" minOccurs="0">
          <xs:complexType>
            <xs:sequence>
              <xs:element type="xs:string" name="firstname"/>
              <xs:element type="xs:string" name="lastname"/>
              <xs:element type="xs:string" name="title"/>
              <xs:element type="xs:string" name="division"/>
              <xs:element type="xs:short" name="building"/>
              <xs:element type="xs:byte" name="room" maxOccurs="unbounded" minOccurs="0"/>
              <xs:element name="address" maxOccurs="unbounded" minOccurs="0">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element type="xs:string" name="city"/>
                    <xs:element type="xs:string" name="pin"/>
                    <xs:element name="addr1">
                      <xs:complexType>
                        <xs:sequence>
                          <xs:element type="xs:string" name="state"/>
                          <xs:element type="xs:string" name="country"/>
                        </xs:sequence>
                      </xs:complexType>
                    </xs:element>
                  </xs:sequence>
                  <xs:attribute type="xs:byte" name="id" use="optional"/>
                </xs:complexType>
              </xs:element>
              <xs:element name="book" minOccurs="0">
                <xs:complexType>
                  <xs:sequence>
                    <xs:element type="xs:string" name="title"/>
                    <xs:element type="xs:string" name="author"/>
                    <xs:element type="xs:short" name="year"/>
                    <xs:element type="xs:float" name="price"/>
                  </xs:sequence>
                  <xs:attribute type="xs:string" name="category"/>
                </xs:complexType>
              </xs:element>
            </xs:sequence>
          </xs:complexType>
        </xs:element>
        <xs:element name="book">
          <xs:complexType>
            <xs:sequence>
              <xs:element type="xs:string" name="title"/>
              <xs:element type="xs:string" name="author"/>
              <xs:element type="xs:short" name="year"/>
              <xs:element type="xs:float" name="price"/>
            </xs:sequence>
            <xs:attribute type="xs:string" name="order"/>
          </xs:complexType>
        </xs:element>
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

这里我的目的是获取特定于元素的属性。在上面的xsd中,我们有一个名为address的元素,它有一个名为id的属性,就像我们有另一个名为category和order的元素book一样。
最后,我想从上面的xsd中按顺序提取以下信息。

firstname
lastname
title
division
building
address_id [Id is the attribute of element address]
address_city [city,pin are complex types]
address_pin
address_addr1_state
address_addr1_country
book_category [category is the attribute of element book]
book_order [order is the attribute of element book]
book_title
book_author
book_year
book_price

我已经完成了解析和验证,这是我试图获取/提取所有元素的代码片段,但我正在以所需的方式获得结果。

val list = doc.getElementsByTagName("xs:element")
for (i <- 0 until list.getLength) {
        val first = list.item(i).asInstanceOf[Element]
        if (first.hasAttributes) {
          val name = first.getAttribute("name")
          val nameType = first.getAttribute("type")
          if (nameType.isEmpty && isColumnValidToAddToList) {
            isColumnsAddedToList = true
            columnPrepend=columnPrepend.concat(name+"_")
            columnXPathPrepend=columnXPathPrepend.concat(name+"/")
          } else if (nameType.isEmpty) {
            isColumnsAddedToList = true
            rootAndRowList = name :: rootAndRowList
          }
          else {
            isColumnsAddedToList = false
          }
          if (!isColumnsAddedToList) {
            isColumnValidToAddToList = true
            columnList = columnPrepend.concat(name) :: columnList
            xpathList = columnXPathPrepend.concat(name) :: xpathList
          }
        }
      }

这是我收到的无效订单。

firstname
lastname
title
division
building
room
address_city
address_pin
address_addr1_state
address_addr1_country
address_addr1_book_title - All are invalid from here
address_addr1_book_author
address_addr1_book_year
address_addr1_book_price
address_addr1_book_book_title
address_addr1_book_book_author
address_addr1_book_book_year
address_addr1_book_book_price

谁能建议我按上述顺序提取正确的信息吗。提前谢谢。

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题