Search code examples
javaregexxmldata-structuresstax

How to Remove newline characters from List<String>


I've got method which returns me a Map from an XML file. I've converted that map to separate Keys and Values into List.

However I'm noticing there are newline characters in the values list. How can I strip out the newline and replace them with a space or leave them blank.

Code:

@Test
public void testGetXMLModelData() throws Exception {
    File f = new File("xmlDir/example.xml");
    Model m = getXMLModelData(f);

    logger.debug("Models Keys: "+m.getInputs());
    logger.debug("Models Values: "+m.getValues());
}

public Model getXMLModelData(File f) throws Exception { 

    Model model = new Model();

    Map<String,String> map = p(f);
    List<String> listKeys = new ArrayList<String>(map.keySet());
    List<String> listValues = new ArrayList<String>(map.values());

    model.setInputs(listKeys);
    model.setValues(listValues); 

    return model;
}


public Map<String, String> p(File file) throws Exception {

    Map<String, String> map = new HashMap<String,String>();
    XMLStreamReader xr = XMLInputFactory.newInstance().createXMLStreamReader(new FileInputStream(file));

    while(xr.hasNext()) {

        int e = xr.next();
        if (e == XMLStreamReader.START_ELEMENT) {
            String name = xr.getLocalName();
            xr.next();
            String value = null;
            try {
                value = xr.getText();
            } catch (IllegalStateException exep) {
                exep.printStackTrace();
            }
            map.put(name, value);
        } 
    }
    return map;
}

Output:

2015-08-19 20:13:52,327 : Models Keys: [IRS1095A, MonthlyPlanPremiumAmtPP, WagesSalariesAndTipsAmt, MonthlyAdvancedPTCAmtPP, MonthCdPP, ReturnData, IndividualReturnFilingStatusCd, PrimaryResidentStatesInfoGrpPP, MonthlyPTCInformationGrpPP, IRS1040, ResidentStateInfoPP, SelfSelectPINGrp, MonthlyPremiumSLCSPAmtPP, Filer, ResidentStateAbbreviationCdPP, PrimaryBirthDt, Return, ReturnHeader, TotalExemptionsCnt, AdjustedGrossIncomeAmt, PrimarySSN]
2015-08-19 20:13:52,328 : Models Values: [
      , 136, 22000, 125, SEPTEMBER, 
    , 1, 
        , 
        , 
      , 
          , 
      , 250, 
      , CA, 1970-01-01, 
  , 
    , 1, 22000, 555-11-2222]

Any help or assistance would be much appreciated. Thanks in advance

Edit:

XML file

<Return xmlns="http://www.irs.gov/efile">
  <ReturnData>
    <IRS1095A uuid="a77f40a2-af31-4404-a27d-4c1eaad730c2">
      <MonthlyPTCInformationGrpPP uuid="69dc9dd5-5415-4ee4-a199-19b2dbb701be">
        <MonthlyPlanPremiumAmtPP>136</MonthlyPlanPremiumAmtPP>
        <MonthlyAdvancedPTCAmtPP>125</MonthlyAdvancedPTCAmtPP>
        <MonthCdPP>SEPTEMBER</MonthCdPP>
        <MonthlyPremiumSLCSPAmtPP>250</MonthlyPremiumSLCSPAmtPP>
      </MonthlyPTCInformationGrpPP>
    </IRS1095A>
    <IRS1040>
      <IndividualReturnFilingStatusCd>1</IndividualReturnFilingStatusCd>
      <WagesSalariesAndTipsAmt>22000</WagesSalariesAndTipsAmt>
      <TotalExemptionsCnt>1</TotalExemptionsCnt>
      <AdjustedGrossIncomeAmt>22000</AdjustedGrossIncomeAmt>
    </IRS1040>
  </ReturnData>
  <ReturnHeader>
    <SelfSelectPINGrp>
      <PrimaryBirthDt>1970-01-01</PrimaryBirthDt>
    </SelfSelectPINGrp>
    <Filer>
      <PrimarySSN>555-11-2222</PrimarySSN>
      <PrimaryResidentStatesInfoGrpPP>
        <ResidentStateInfoPP uuid="a77f40a2-af31-4404-a27d-4c1eaad730c2">
          <ResidentStateAbbreviationCdPP>CA</ResidentStateAbbreviationCdPP>
        </ResidentStateInfoPP>
      </PrimaryResidentStatesInfoGrpPP>
    </Filer>
  </ReturnHeader>
</Return>

Solution

  • Set value = xr.getText().trim(). That will trim extraneous characters from the beginning and end of the values.

    To then prevent adding the value, wrap the map.put(name, value) with an if (value != null && !value.isEmpty())