如何在 xml 中搜索元素?

How to search an element in xml?

我需要在 .xml 文件中搜索元素。它有这样的结构:

   <elements number="3">
     <contact>
       <name>PAUL</name>
       <code>A1</code>
     </contact>
     <contact>
       <name>LAURA</name>
       <code>A2</code>
     </contact>
     <contact>
       <name>JOHN</name>
       <code>A3</code>
     </contact>

我的解决方案是这样的:

public static String readContactsList(String nameContact) {
       XMLInputFactory xmlif = null;
       XMLStreamReader xmlr = null;

       String contacts = "contacts.xml";

       try {
           xmlif = XMLInputFactory.newInstance();
           xmlr = xmlif.createXMLStreamReader(contacts, new FileInputStream(contacts)); 
            while (xmlr.hasNext()) { 
                switch (xmlr.getEventType()) { 
                case XMLStreamConstants.START_DOCUMENT: 
                    System.out.println("Start Read Doc " + contacts); 
                    break;

                case XMLStreamConstants.START_ELEMENT: 
                    System.out.println("Tag " + xmlr.getLocalName());
                    for (int i = 0; i < xmlr.getAttributeCount(); i++)
                        System.out.printf(" => attribut %s->%s%n", xmlr1.getAttributeLocalName(i), xmlr.getAttributeValue(i)); 
                    break;
                case XMLStreamConstants.END_ELEMENT: 
                    System.out.println("END-Tag " + xmlr.getLocalName()); 
                    break;
                case XMLStreamConstants.COMMENT:
                    System.out.println("// comment " + xmlr.getText()); 
                    break; 
                case XMLStreamConstants.CHARACTERS: 
                    if (xmlr.getText().trim().length() > 0) 
                    if (!xmlr.getText().equals(nameContact)) {
                        xmlr.next();
                    }
                    else 
                    System.out.println("-> " + xmlr.getText()); 
                    break;
            }
               xmlr.next();
            }
       } 
       catch (Exception e) {
           System.out.println("reader initialization error");
           System.out.println(e.getMessage()); 
           }
       return contacts;
   }

此方法接收一个联系人的姓名(即 "LAURA"),它应该 return 它的代码。我想念的是如何滚动 .xml 文件以及如何访问不同的标签内容。

谢谢!

以下代码(基于您的代码)应该可以工作,但请注意,如果您调换标签 <name><code> 的顺序,它将失败。

另外,请注意您并没有关闭流。只需放置一个 finally {xmlr.close();} 即可关闭流。

   public static String readContactsList(String nameContact) {
        boolean match = false;   
        XMLInputFactory xmlif = null;
        XMLStreamReader xmlr = null;

        String contacts = "contacts.xml";

        try {
            xmlif = XMLInputFactory.newInstance();
            xmlr = xmlif.createXMLStreamReader(contacts, new FileInputStream(contacts)); 
            while (xmlr.hasNext()) { 
                switch (xmlr.getEventType()) { 
                case XMLStreamConstants.START_DOCUMENT: 
                    System.out.println("Start Read Doc " + contacts); 
                    break;

                case XMLStreamConstants.START_ELEMENT: 
                    System.out.println("Tag " + xmlr.getLocalName());    
                    for (int i = 0; i < xmlr.getAttributeCount(); i++)
                        System.out.printf(" => attribut %s->%s%n", xmlr.getAttributeLocalName(i), xmlr.getAttributeValue(i));
                    break;

                case XMLStreamConstants.END_ELEMENT: 
                    System.out.println("END-Tag " + xmlr.getLocalName()); 
                    break;

                case XMLStreamConstants.COMMENT:
                    System.out.println("// comment " + xmlr.getText()); 
                    break; 

                case XMLStreamConstants.CHARACTERS: 
                    if (xmlr.getText().trim().length() > 0) {
                        if (!xmlr.getText().equals(nameContact)) {
                            System.out.println("-> " + xmlr.getText());

                            if (match) {
                                return xmlr.getText();
                            }
                        }
                        else {
                            System.out.println("-> " + xmlr.getText());

                            //This is just in case the name and the code be the same
                            if (match) {
                                return xmlr.getText();
                            }

                            match = true;
                        }
                    }
                    break;
                }
                xmlr.next();
            }
        } 
        catch (Exception e) {
            System.out.println("reader initialization error");
            System.out.println(e.getMessage()); 
        }
        return contacts;
    }