Java中的Stax仅返回特定属性

时间:2017-05-24 21:39:48

标签: java xml parsing stax

我希望只能检索属性UnitID = 7854的信息,这样程序才能循环遍历该属性中的XML数据,并只获取该ITEM的信息。

我有一个名为employee.xml的XML,如下所示:

<inventory>
    <item UnitID="1234" Record="0">
        <id>1234</id>
        <dealerid>455</dealerid>
        <stock_number>1600Xtreme</stock_number>

        <details>This is some additional details &nbsp; about the 
        product</details>

        <make>Nvidia</make>                       
    </item>
    <item UnitID="7854" Record="1">
        <id>7854</id>
        <dealerid>587</dealerid>
        <stock_number>12TMAX5500</stock_number>

        <details>This is some additional details &nbsp; about the 
        product</details>

        <make>Realtek</make> 
    </item>
</inventory>

到目前为止我有这个JAVA代码

public class StaxParserTests {

        static int userInput = 7854;

         public static void main(String[] args) throws Throwable {
                String fileName = "/Users/david/eclipse/workspace/StructData_Len/src/Test1/feedForTests.xml";
                //String fileName = "/Users/david/eclipse/workspace/StructData_Len/src/Test3/employee.xml";

                XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
                xmlInputFactory.setProperty(javax.xml.stream.XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES, false);

                try {
                    XMLStreamReader xMLStreamReader = xmlInputFactory.createXMLStreamReader(new FileInputStream(fileName));

                    while(xMLStreamReader.hasNext()){
                  /*e*/ 
                  int eventType = xMLStreamReader.getEventType();

                  switch(eventType){
                  case XMLStreamReader.START_ELEMENT:
                      System.out.println("Start found: " + xMLStreamReader.getLocalName());




                          if(xMLStreamReader.getLocalName().equals("item")){
                              System.out.println("emplyoee found ! ");
                              long id = Integer.parseInt(xMLStreamReader.getAttributeValue(null, "UnitID"));
                                  if(id == userInput){
                                      System.out.println("we found attrib userInput : " + xMLStreamReader.getAttributeValue(0));

                                      final String idTag = "id";
                                      final String dealeridTag = "dealerid";

                                      xMLStreamReader.nextTag();

                                      while(xMLStreamReader.hasNext()){
                                      switch(xMLStreamReader.getLocalName()){
                                      case idTag:  
                                      System.out.println("id tag found. Value: " + xMLStreamReader.getElementText() );
                                      break;
                                      case dealeridTag:  
                                          System.out.println("dealerid tag found. Value: " + xMLStreamReader.getElementText() );
                                          break;
                                      }
                                      }
 }


                      }

                           break;


                  }
                  xMLStreamReader.next();

                    }


                    xMLStreamReader.close();
                }catch (FileNotFoundException | XMLStreamException e) {
                           e.printStackTrace();


                       }
         }

    }

所以目标是StreamReader可以通过项目的标签&#34; 7854&#34;如果它找到,例如,它将它的ElementText存储在一个变量中,以便以后格式化和输出。

请给我一些指导......谢谢!

0 个答案:

没有答案