I’m trying to parse an XML file with big text inside of the tags. Sometimes the texts have carriage returns in them. When I debug, my String gets overwritten with a carriage return (“\n”) which sometimes leads to an ampty string or a string with just one line.
I have no control on how the data is stored in the database, I can just read it in XML. Some people put a carriage return behind each tag, this leads to the empty strings.
Example of an XML file:
<?xml version="1.0" encoding="utf-8" ?>
<vacaturedetails>
<details>
<titel>MEDEWERKER VOOR ONDERHOUDSDIENST</titel>
<werkveld>METAALMECHANICA</werkveld>
<regio>Regio Roeselare- Izegem</regio>
<tewerkstellingsplaats>Bedrijf in Roeselare met goeie reputatie.</tewerkstellingsplaats>
<diploma1>A2 (Beroeps + 7ej, Technisch, ASO)</diploma1>
<diploma2>A3 (Beroeps tot 6e j, Deeltijds, Leercontract)</diploma2>
<taal1>Nederlands</taal1>
<ervaring>6 maand - 2 jaar</ervaring>
<rijbewijs>B</rijbewijs>
<rijbewijsOmsch>Auto</rijbewijsOmsch>
<omschrijving>- Inzicht in, en zelfstandig kunnen monteren en lassen van metalen constructies. - Hulp bij het ontwikkelen van nieuwe constructies van onderdelen in de productielijn of verbeteren van bestaande constructies. - Kunnen rijden met heftruck en werken met hoogtewerker. - Plaatsen van leidingen voor perslucht, water, ...en sanitair. - Kleine herstellingen uitvoeren. - Hulp bij verhuis binnen het bedrijf. - Allerhande klusjes die nodig zijn aan en rond de bedrijfsgebouwen. </omschrijving>
<aanbod>Dagwerk met een goeie verloning in een goei werksfeer. Optie vast na uitzendperiode. </aanbod>
<profiel>- Kunnen lassen met halfautomaat, kennis autogeen lassen is een pluspunt. - Kunnen rijden met heftruck (attest is een pluspunt) en werken met hoogtewerker. - Inzicht hebben en zelfstandig kunnen monteren van constructies. - Kennis van sanitair (water, gas, perslucht) - Ervaring is een must!</profiel>
</details>
</vacaturedetails>
This is how my parser looks:
package stage.accent.webservice;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import stage.accent.domain.VacatureDetails;
public class vacatureDetailsWebservice extends DefaultHandler {
private boolean vacaturedetailstag = false;
private boolean detailstag = false;
private boolean titeltag = false;
private boolean werkveldtag = false;
private boolean tewerkstellingsplaatstag = false;
private boolean diploma1tag = false;
private boolean diploma2tag = false;
private boolean taal1tag = false;
private boolean taal2tag = false;
private boolean taal3tag = false;
private boolean taal4tag = false;
private boolean taal5tag = false;
private boolean ervaringtag = false;
private boolean omschrijvingtag = false;
private boolean aanbodtag = false;
private boolean profieltag = false;
private boolean rijbewijstag = false;
private boolean rijbewijsOmschtag = false;
private String test;
//private Vacature vacature = new Vacature();
private VacatureDetails details;
public VacatureDetails getVacatures() {
return this.details;
}
@Override
public void startDocument() throws SAXException {
this.details = new VacatureDetails();
}
@Override
public void endDocument() throws SAXException {
// Nothing to do
}
/** Gets be called on opening tags like:
* <tag>
* Can provide attribute(s), when xml was like:
* <tag attribute="attributeValue">*/
@Override
public void startElement(String namespaceURI, String localName,
String qName, Attributes atts) throws SAXException {
if (localName.equals("vacaturedetails")) {
this.vacaturedetailstag = true;
}else if (localName.equals("details")) {
this.detailstag = true;
}else if (localName.equals("titel")) {
this.titeltag = true;
}else if (localName.equals("werkveld")){
this.werkveldtag = true;
}else if (localName.equals("tewerkstellingsplaats")){
this.tewerkstellingsplaatstag = true;
}else if (localName.equals("diploma1")){
this.diploma1tag = true;
}else if (localName.equals("diploma2")){
this.diploma2tag = true;
}else if (localName.equals("taal1")){
this.taal1tag = true;
}else if (localName.equals("taal2")){
this.taal2tag = true;
}else if (localName.equals("taal3")){
this.taal3tag = true;
}else if (localName.equals("taal4")){
this.taal4tag = true;
}else if (localName.equals("taal5")){
this.taal5tag = true;
}else if (localName.equals("ervaring")){
this.ervaringtag = true;
}else if (localName.equals("rijbewijs")){
this.rijbewijstag = true;
}else if (localName.equals("rijbewijsOmsch")){
this.rijbewijsOmschtag = true;
}else if (localName.equals("omschrijving")){
this.omschrijvingtag = true;
}else if (localName.equals("aanbod")){
this.aanbodtag = true;
}else if (localName.equals("profiel")){
this.profieltag = true;
}
}
/** Gets be called on closing tags like:
* </tag> */
@Override
public void endElement(String namespaceURI, String localName, String qName)
throws SAXException {
if (localName.equals("vacaturedetails")) {
this.vacaturedetailstag = false;
}else if (localName.equals("details")) {
this.detailstag = false;
}else if (localName.equals("titel")) {
this.titeltag = false;
}else if (localName.equals("werkveld")){
this.werkveldtag = false;
}else if (localName.equals("tewerkstellingsplaats")){
this.tewerkstellingsplaatstag = false;
}else if (localName.equals("diploma1")){
this.diploma1tag = false;
}else if (localName.equals("diploma2")){
this.diploma2tag = false;
}else if (localName.equals("taal1")){
this.taal1tag = false;
}else if (localName.equals("taal2")){
this.taal2tag = false;
}else if (localName.equals("taal3")){
this.taal3tag = false;
}else if (localName.equals("taal4")){
this.taal4tag = false;
}else if (localName.equals("taal5")){
this.taal5tag = false;
}else if (localName.equals("ervaring")){
this.ervaringtag = false;
}else if (localName.equals("rijbewijs")){
this.rijbewijstag = false;
}else if (localName.equals("rijbewijsOmsch")){
this.rijbewijsOmschtag = false;
}else if (localName.equals("omschrijving")){
this.omschrijvingtag = false;
}else if (localName.equals("aanbod")){
this.aanbodtag = false;
}else if (localName.equals("profiel")){
this.profieltag = false;
}
}
/** Gets be called on the following structure:
* <tag>characters</tag> */
@Override
public void characters(char ch[], int start, int length) {
if(this.titeltag){
details.setTitel(new String(ch, start, length));
}
if(this.werkveldtag){
details.setWerkveld(new String(ch, start, length));
}
if(this.tewerkstellingsplaatstag){
details.setTewerkstellingsplaats(new String(ch, start, length));
}
if(this.diploma1tag){
details.setDiploma1(new String(ch, start, length));
}
if(this.diploma2tag){
details.setDiploma2(new String(ch, start, length));
}
if(this.taal1tag){
details.setTaal1(new String(ch, start, length));
}
if(this.taal2tag){
details.setTaal2(new String(ch, start, length));
}
if(this.taal3tag){
details.setTaal3(new String(ch, start, length));
}
if(this.taal4tag){
details.setTaal4(new String(ch, start, length));
}
if(this.taal5tag){
details.setTaal5(new String(ch, start, length));
}
if(this.ervaringtag){
details.setErvaring(new String(ch, start, length));
}
if(this.rijbewijstag){
details.setRijbewijs(new String(ch, start, length));
}
if(this.rijbewijsOmschtag){
details.setRijbewijsOmsch(new String(ch, start, length));
}
if(this.omschrijvingtag){
details.setOmschrijving(new String(ch, start, length));
}
if(this.aanbodtag){
details.setAanbod(new String(ch, start, length));
}
if(this.profieltag){
details.setProfiel(new String(ch, start, length));
}
test = details.toString();
}
}
The output of this Example of omschrijving was = - Allerhande klusjes die nodig zijn aan en rond de bedrijfsgebouwen.
Is there any way to parse this, so that the full text with carriage returns appear in my String.
I understand that you overwrite the value of that tag always with the last chunk.
Replace this piece of code in the method
characterswith
EDIT: basically, what you do is you check whether the value of Omschrijving is set or not and act accordingly. In case this code confuses you, check out the same thing as a little bit a different expression
So you check whether the value of Omschrijving is empty and if it’s not, you concatenate the already existing value, otherwise you just assign the new value. That’s pretty much it.