0

实际上,我试图仅获取一个特定节点的值,但节点标记位于 xml 文档中的许多地方。这是xml片段

<t:Address xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types"><t:Name>XYZ Conference Rooms</t:Name><t:EmailAddress>XYZConferenceRooms@abc.co.in</t:EmailAddress><t:RoutingType>SMTP</t:RoutingType><t:MailboxType>PublicDL</t:MailboxType></t:Address><t:Address xmlns:t="http://schemas.microsoft.com/exchange/services/2006/types"><t:Name>ABC Conference Rooms</t:Name><t:EmailAddress>ABCConferenceRooms@abc.co.in</t:EmailAddress><t:RoutingType>SMTP</t:RoutingType><t:MailboxType>PublicDL</t:MailboxType></t:Address>

问题是我只想要来自上述 xml 的值(ABCConferenceRooms@abc.co.in)。以上只是一个代码片段,我们在 xml 中有 10 个电子邮件地址值。

当我在我的解析器中这样做时

    String value;
String url;
String confName;
String confEmail;

@Override
public void startElement(String uri, String localName, String qName,
        Attributes attributes) throws SAXException {

    value = qName;
     confEmail = qName;
     confName = qName;
}

@Override
public void characters(char[] ch, int start, int length)
        throws SAXException {
    if (value.equals("Value")) {
        String OperationName1Text = new String(ch, start, length);
        info.setValue(OperationName1Text);
        Log.d("URL", OperationName1Text);
    }
     else if (confName.equals("t:EmailAddress")) {
     String getEmail = new String(ch, start, length);
     info.setEmail(getEmail);
     Log.d("Email", getEmail);
     }
}

@Override
public void endElement(String uri, String localName, String qName)
        throws SAXException {
    value = qName;
    confEmail = qName;
    confName = qName;
}

我只能显示所有的电子邮件地址,但我只想要上面提到的一个。需要对代码进行哪些更改。?

任何在类似情况下工作的人都可以提供帮助谢谢迈克

4

2 回答 2

0

当您在两个地址节点中使用相同的命名空间时,它们也将被继承到电子邮件节点。因此,命名空间没有区别,您无法找到确切的节点来获得所需的内容。如果您知道电子邮件节点的位置(第二个电子邮件节点等)然后您可以在 startElement() 中有一个计数器,当计数器找到第二个电子邮件节点时,然后在其中获取相关文本。

于 2013-06-16T04:53:02.270 回答
0

您是否为您的工作尝试过 SAX XML Parser。如果没有,请尝试一次...

您的 XML 文件是:

<?xml version="1.0"?>
<company>
    <staff>
        <firstname>yong</firstname>
        <lastname>mook kim</lastname>
        <nickname>mkyong</nickname>
        <salary>100000</salary>
    </staff>
    <staff>
        <firstname>low</firstname>
        <lastname>yin fong</lastname>
        <nickname>fong fong</nickname>
        <salary>200000</salary>
    </staff>
</company>

XML 解析器的 JAVA 代码

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;

public class ReadXMLFile {

   public static void main(String argv[]) {

    try {

    SAXParserFactory factory = SAXParserFactory.newInstance();
    SAXParser saxParser = factory.newSAXParser();

    DefaultHandler handler = new DefaultHandler() {

    boolean bfname = false;
    boolean blname = false;
    boolean bnname = false;
    boolean bsalary = false;

    public void startElement(String uri, String localName,String qName, 
                Attributes attributes) throws SAXException {

        System.out.println("Start Element :" + qName);

        if (qName.equalsIgnoreCase("FIRSTNAME")) {
            bfname = true;
        }

        if (qName.equalsIgnoreCase("LASTNAME")) {
            blname = true;
        }

        if (qName.equalsIgnoreCase("NICKNAME")) {
            bnname = true;
        }

        if (qName.equalsIgnoreCase("SALARY")) {
            bsalary = true;
        }

    }

    public void endElement(String uri, String localName,
        String qName) throws SAXException {

        System.out.println("End Element :" + qName);

    }

    public void characters(char ch[], int start, int length) throws SAXException {

        if (bfname) {
            System.out.println("First Name : " + new String(ch, start, length));
            bfname = false;
        }

        if (blname) {
            System.out.println("Last Name : " + new String(ch, start, length));
            blname = false;
        }

        if (bnname) {
            System.out.println("Nick Name : " + new String(ch, start, length));
            bnname = false;
        }

        if (bsalary) {
            System.out.println("Salary : " + new String(ch, start, length));
            bsalary = false;
        }

    }

     };

       saxParser.parse("c:\\file.xml", handler);

     } catch (Exception e) {
       e.printStackTrace();
     }

   }

}

你的输出是:

Start Element :company
Start Element :staff
Start Element :firstname
First Name : yong
End Element :firstname
Start Element :lastname
Last Name : mook kim
End Element :lastname
Start Element :nickname
Nick Name : mkyong
End Element :nickname
Start Element :salary
Salary : 100000
End Element :salary
End Element :staff
Start Element :staff
Start Element :firstname
First Name : low
End Element :firstname
Start Element :lastname
Last Name : yin fong
End Element :lastname
Start Element :nickname
Nick Name : fong fong
End Element :nickname
Start Element :salary
Salary : 200000
End Element :salary
End Element :staff
End Element :company
于 2013-06-16T05:24:53.687 回答