`
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[] args) {
// TODO Auto-generated method stub
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
DefaultHandler handler = new DefaultHandler() {
boolean bauthor = false;
//boolean bkey = false;
public void startElement(String uri, String localName,String qName,Attributes attributes) throws SAXException {
if (qName.equalsIgnoreCase("author") ) {
bauthor = true;
}
}
public void characters(char ch[], int start, int length) throws SAXException {
if (bauthor) {
System.out.println("Co-auteurs : " + new String(ch, start, length));
bauthor = false;
}
}
};
saxParser.parse("test.xml", handler);
} catch (Exception e) {
e.printStackTrace();
}
}
}
`
Я хочу для Exemple получить всех авторов, которые имеют (KEY =" phd / dk / Heine2010 ") из этого XML-файла (test.xml), кого-топомогите мне, мой код может просто выбрать всех авторов:
<?xml version="1.0" encoding="UTF-8"?>
<dblp>
<phdthesis mdate="2016-05-04" key="phd/dk/Heine2010">
<author>Carmen Heine</author>
</phdthesis>
<phdthesis mdate="2017-01-06" key="phd/Hoff2002">
<author>Gerd Hoff</author>
</phdthesis>
<phdthesis mdate="2019-07-10" key="phd/dk/Heine2010">
<author>Margo I. Seltzer</author>
</phdthesis>
<phdthesis mdate="2017-01-06" key="phd/Schiwietz93">
<author>Michael Schiwietz</author>
</phdthesis>
<phdthesis mdate="2016-05-04" key="phd/dk/Heine2010">
<author>Carmen Heine</author>
</phdthesis>
</dblp>
* Я хочу в качестве примера получить всех авторов, имеющих одинаковый (KEY = "phd / dk / Heine2010") из этого XMLФайл (test.xml) кому-то поможет, мой код может просто выбрать всех авторов *