...">

Good way to parse XML like this sample?

I am wondering what is the best way to parse XML as follows:

<root>
    <MailNotification enable="true">
        <To>foo@bar.org</To>
        <From>foo@bar.org</From>
        <Server>smtp.bar.org</Server>
        <Port>465</Port>
        <Username>foo@bar.org</Username>
        <Password>fooo!</Password>
    </MailNotification>
</root>

I am using Java 7, the full XML is longer, but it is not a very large file. I was thinking about using Stax Pull Parser because it seemed easy, but there is one thing when I'm not sure if this is really a good way:

When navigating to the MailNotification element, I could, for example, create a new instance, for example, a mail class, I have no problem with this. But: What if I come, for example. to the element To? How do I know if it really is inside the MailNotification element, and not directly under the root? In other words: what I am missing is the best practice for handling states such as "I am now in the MailNotification element."

. , XML, , To MailNotification To , - : - /, , To.

!

+3
6

StAX Stream Reader - . Java-, , . XMLStreamConstants.

XMLStreamReader reader;

void parseRoot() {
    reader.require(START_ELEMENT, null, "root");

    while (reader.nextTag() == START_ELEMENT) {
        switch (reader.getLocalName()) {
        case "MailNotification":
            MailNotification mail = parseMail();
            // do something with mail
            break;
        // more cases
        }
    }

    reader.require(END_ELEMENT, null, "root");
}

MailNotification parseMail() {
    reader.require(START_ELEMENT, null, "MailNotification");
    MailNotification mail = new MailNotification();

    while (reader.nextTag() == START_ELEMENT) {
        switch (reader.getLocalName()) {
        case "To":
            mail.setTo(parseString());
            break;
        // more cases
        }
    }

    reader.require(END_ELEMENT, null, "MailNotification");
    return mail;
}

String parseString() {
    String text = "";
    if (reader.next() == CHARACTERS) {
        text = reader.getText();
        reader.next();
    }
    return text;
}

(*) " ", , .
JAXB , XML , .
JDOM , XML , , - XPath; . , .
SAX , StAX .

+4

Digester.

public static final String TEST_XML = "<root>\n" +
          "<MailNotification>\n" +
          " <to>foo@bar.org</to>\n" +
          " <from>foo@bar.org</from>\n" +
          " </MailNotification>\n" +
          "</root>";



Digester digester = new Digester();
digester.setValidating(false);

digester.addObjectCreate("root/MailNotification", MailNotification.class);
digester.addBeanPropertySetter("root/MailNotification/to", "to");
digester.addBeanPropertySetter("root/MailNotification/from", "from");

MailNotification notification = (MailNotification) digester.parse(new StringReader(TEST_XML));
System.out.println(notification.getTo());
System.out.println(notification.getFrom());



public class MailNotification {
  private String to;
  private String from;

  public String getTo() {
    return to;
  }

  public void setTo(String to) {
    this.to = to;
  }

  public String getFrom() {
    return from;
  }

  public void setFrom(String from) {
    this.from = from;
  }
+2

JAXB? java unmarshall, .

+1

:

XML

, / .

i.e:

How do I know if it is really inside a MailNotification element and not directly below the root?

start/end .

0

XML. "To" "MailNotification".

, . . jdom , , . , .

0

, XML, , , : , , "StAX " " JAXB", , . , , , , .

, , JDOM.

0

All Articles