Reputation: 109
I have a XML file, I'm reading it's content using BufferedReader, i then store some pieces of information in String using substring. See following code:
Load file, basically I take whole xml file and store it in String called whole XML
try {
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(new FileInputStream(inputFile), "UTF-8"));
while ((line2 = bufferedReader.readLine()) != null) {
wholeXML= line2;
} catch (IOException ex2) {
System.out.println("Exception xml");
}
after that i use substring to get data i need for example:
String senderID = wholeXML.substring(wholeXML.indexOf("<q1:SenderID>")+13,wholeXML.indexOf("</q1:SenderID>"));`
This serves my purpose and workes just fine, but i'm having problem because one part in xml file is not static it's dynamic, like this:
q1:Attachment>
<q1:AttachmentID>ba9727cc-a831-4ded-b88c-a00000041357</q1:AttachmentID>
</q1:Attachment>
-<q1:Attachment>
<q1:AttachmentID>c0773e77-e011-484e-a1e9-b00000131099</q1:AttachmentID>
</q1:Attachment>
-<q1:Attachment>
<q1:AttachmentID>08f57403-2feb-443c-8dd4-b00000131103</q1:AttachmentID>
</q1:Attachment>
-<q1:Attachment>
<q1:AttachmentID>53c47aba-bb64-4349-a0dc-b00000131105</q1:AttachmentID>
</q1:Attachment>
-<q1:Attachment>
<q1:AttachmentID>3ee501ed-5c5c-43ab-8bd0-b00000131108</q1:AttachmentID>
</q1:Attachment>
-<q1:Attachment>
<q1:AttachmentID>d4fe537a-a95a-4902-a583-b00000131112</q1:AttachmentID>
So as you can see there are multiple tags with the same name and I need to store data inside of them, but I don't know how many there will be, given it's different for each XML file. I'm a beginner so please go easy on me if there is an obvious solution, I'm just not seeing it.
Upvotes: 0
Views: 346
Reputation: 1982
Your approach (substring matching on the XML string) is not advisable, you should use one of the XML parsing methods available in Java (SAX, DOM, StAX, JAXB, see Which is the best library for XML parsing in java).
Example using SAX:
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import javax.xml.stream.XMLStreamException;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class StaxExample {
public static class CustomSAXHandler extends DefaultHandler {
private String senderId;
private final List<String> attachmentIds = new ArrayList<>();
private StringBuffer currentCharacters = new StringBuffer();
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
if (currentCharacters != null) {
currentCharacters.append(String.valueOf(ch, start, length));
}
}
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes)
throws SAXException {
currentCharacters = new StringBuffer();
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
switch (localName) {
case "AttachmentID":
getAttachmentIds().add(currentCharacters.toString());
break;
case "SenderID":
setSenderId(currentCharacters.toString());
break;
}
currentCharacters = null;
}
public String getSenderId() {
return senderId;
}
public void setSenderId(String senderId) {
this.senderId = senderId;
}
public List<String> getAttachmentIds() {
return attachmentIds;
}
}
public static void main(String[] args) throws XMLStreamException, SAXException, IOException, ParserConfigurationException {
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setValidating(true);
factory.setNamespaceAware(true);
SAXParser saxParser = factory.newSAXParser();
CustomSAXHandler saxHandler = new CustomSAXHandler();
saxParser.parse(StaxExample.class.getResourceAsStream("test.xml"), saxHandler);
System.out.println("SenderID: " + saxHandler.getSenderId());
System.out.println("AttachmentIDs: " + saxHandler.getAttachmentIds());
}
}
Explanation:
Parsing a document with SAX requires you to provide a SAX handler, in which you can override certain methods to react on encounters of the different XML elements.
I created a fairly simple custom SAX handler which just records encountered text and stores it in instance variables (senderId, attachmentIds) for later retrieval.
As you see, the senderId is a single String (as it is expected to be encountered only once), and the attachmentIds is a List of Strings to be able to store multiple occurrences.
Upvotes: 1