如何使用Stax解析器解析具有重复xml标记名称和多个条件的xml?

时间:2017-05-17 04:38:47

标签: java xml xml-parsing stax

我正在尝试解析Text元素的值,但正如您所看到的,我在myNote和yourNote中都有多个Text元素。如何使用Stax xml解析器解析这些Text值? Plz帮忙!!!我只需要使用Stax解析器就可以了。

public class StaxParser {

    public static void main(String[] args) throws FileNotFoundException {
        String fileName = "/Users/ragnar/Documents/tweet.xml";
        parseXML(fileName);

    }

    private static void parseXML(String fileName) throws FileNotFoundException {
        StaxHelper st = new StaxHelper();
        XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
        try {
            XMLEventReader xmlEventReader = xmlInputFactory
                    .createXMLEventReader(new FileInputStream(fileName));
            while (xmlEventReader.hasNext()) {
                XMLEvent xmlEvent = xmlEventReader.nextEvent();
                if (xmlEvent.isStartElement()) {
                    StartElement startElement = xmlEvent.asStartElement();
                    if (startElement.getName().getLocalPart().equals("MyNote")) {
                        xmlEvent = xmlEventReader.nextTag();
                        xmlEvent = xmlEventReader.nextEvent();
                        if (xmlEvent.asCharacters().getData().equals("1")) {
                            System.out.println("Number is: " + xmlEvent.asCharacters().getData());
                            // Need to set myNote1 if Number is 1
                            // Need to set myNote2 if Number is 2
                            // Need to set myNote3 if Number is 3
                            // Need to set yourNote1 if Number is 1
                            // Need to set yourNote2 if Number is 2
                            // Need to set yourNote3 if Number is 3
                        }
                    }
                }

                if (xmlEvent.isEndElement()) {
                    EndElement endElement = xmlEvent.asEndElement();
                    if (endElement.getName().getLocalPart().equals("Tweet")) {
                        break;
                    }
                }
            }

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

// StaxHelper Class

public class StaxHelper {

    String myNote1;
    String myNote2;
    String myNote3;
    String yourNote1;
    String yourNote2;
    String yourNote3;

    public String getMyNote1() {
        return myNote1;
    }

    public void setMyNote1(String myNote1) {
        this.myNote1 = myNote1;
    }

    public String getMyNote2() {
        return myNote2;
    }

    public void setMyNote2(String myNote2) {
        this.myNote2 = myNote2;
    }

    public String getMyNote3() {
        return myNote3;
    }

    public void setMyNote3(String myNote3) {
        this.myNote3 = myNote3;
    }

    public String getYourNote1() {
        return yourNote1;
    }

    public void setYourNote1(String yourNote1) {
        this.yourNote1 = yourNote1;
    }

    public String getYourNote2() {
        return yourNote2;
    }

    public void setYourNote2(String yourNote2) {
        this.yourNote2 = yourNote2;
    }

    public String getYourNote3() {
        return yourNote3;
    }

    public void setYourNote3(String yourNote3) {
        this.yourNote3 = yourNote3;
    }
}

    <?xml version="1.0" encoding="UTF-8"?>
<Tweet>
	<MyNotes>
		<MyNote>
			<Number>1</Number>
			<Text>myNote1</Text>
		</MyNote>
		<MyNote>
			<Number>2</Number>
			<Text>myNote2</Text>
		</MyNote>
		<MyNote>
			<Number>3</Number>
			<Text>myNote3</Text>
		</MyNote>
	</MyNotes>
	<YourNotes>
		<YourNote>
			<Number>1</Number>
			<Text>yourNote1</Text>
		</YourNote>
		<YourNote>
			<Number>2</Number>
			<Text>yourNote2</Text>
		</YourNote>
		<YourNote>
			<Number>3</Number>
			<Text>yourNote3</Text>
		</YourNote>
	</YourNotes>
</Tweet>

`

1 个答案:

答案 0 :(得分:0)

  • 我提出了这个解决方案,但是你可以看到我有太多嵌套if else条件。我想要比这更好的解决方案。注意:我知道Switch语句的性能要快得多,但我只能使用Java 1.6版本,因此我不能使用Switch for Strings。

private static void parseXML(String fileName) throws FileNotFoundException {
		StaxHelper st = new StaxHelper();
		XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
		try {
			XMLEventReader xmlEventReader = xmlInputFactory.createXMLEventReader(new FileInputStream(fileName));
			while (xmlEventReader.hasNext()) {
				XMLEvent xmlEvent = xmlEventReader.nextEvent();
				if (xmlEvent.isStartElement()) {
					StartElement startElement = xmlEvent.asStartElement();
					if (startElement.getName().getLocalPart().equals("MyNote")) {
						xmlEvent = xmlEventReader.nextTag();
						xmlEvent = xmlEventReader.nextEvent();
						if (xmlEvent.asCharacters().getData().equals("1")) {
							String number1 = xmlEvent.asCharacters().getData();
							if (number1.equals("1")) {
								xmlEvent = xmlEventReader.nextEvent();
								xmlEvent = xmlEventReader.nextTag();
								if (xmlEvent.isStartElement() == true) {
									xmlEvent = xmlEventReader.nextEvent();
									st.setMyNote1(xmlEvent.asCharacters().getData());
									System.out.println("My Note 1 is: " + st.getMyNote1());
								} 
							} 
						} else if (xmlEvent.asCharacters().getData().equals("2")) {
							String number2 = xmlEvent.asCharacters().getData();
							if (number2.equals("2")) {
								xmlEvent = xmlEventReader.nextEvent();
								xmlEvent = xmlEventReader.nextTag();
								if (xmlEvent.isStartElement() == true) {
									xmlEvent = xmlEventReader.nextEvent();
									st.setMyNote2(xmlEvent.asCharacters().getData());
									System.out.println("My Note 2 is: " + st.getMyNote2());
								} 
							}
						} else if (xmlEvent.asCharacters().getData().equals("3")) {
							String number3 = xmlEvent.asCharacters().getData();
							if (number3.equals("3")) {
								xmlEvent = xmlEventReader.nextEvent();
								xmlEvent = xmlEventReader.nextTag();
								if (xmlEvent.isStartElement() == true) {
									xmlEvent = xmlEventReader.nextEvent();
									st.setMyNote3(xmlEvent.asCharacters().getData());
									System.out.println("My Note 3 is: " + st.getMyNote3());
								} 
							}
						} 
					}
					if (startElement.getName().getLocalPart().equals("YourNote")) {
						xmlEvent = xmlEventReader.nextTag();
						xmlEvent = xmlEventReader.nextEvent();
						if (xmlEvent.asCharacters().getData().equals("1")) {
							String number1 = xmlEvent.asCharacters().getData();
							if (number1.equals("1")) {
								xmlEvent = xmlEventReader.nextEvent();
								xmlEvent = xmlEventReader.nextTag();
								if (xmlEvent.isStartElement() == true) {
									xmlEvent = xmlEventReader.nextEvent();
									st.setYourNote1(xmlEvent.asCharacters().getData());
									System.out.println("Your Note 1 is: " + st.getYourNote1());
								} 
							} 
						} else if (xmlEvent.asCharacters().getData().equals("2")) {
							String number2 = xmlEvent.asCharacters().getData();
							if (number2.equals("2")) {
								xmlEvent = xmlEventReader.nextEvent();
								xmlEvent = xmlEventReader.nextTag();
								if (xmlEvent.isStartElement() == true) {
									xmlEvent = xmlEventReader.nextEvent();
									st.setYourNote2(xmlEvent.asCharacters().getData());
									System.out.println("Your Note 2 is: " + st.getYourNote2());
								} 
							}
						} else if (xmlEvent.asCharacters().getData().equals("3")) {
							String number3 = xmlEvent.asCharacters().getData();
							if (number3.equals("3")) {
								xmlEvent = xmlEventReader.nextEvent();
								xmlEvent = xmlEventReader.nextTag();
								if (xmlEvent.isStartElement() == true) {
									xmlEvent = xmlEventReader.nextEvent();
									st.setYourNote3(xmlEvent.asCharacters().getData());
									System.out.println("Your Note 3 is: " + st.getYourNote3());
								} 
							}
						} 
					}
				}

				if (xmlEvent.isEndElement()) {
					EndElement endElement = xmlEvent.asEndElement();
					if (endElement.getName().getLocalPart().equals("Tweet")) {
						break;
					}
				}
			}

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