如何从列表中删除换行符<String>
How to Remove newline characters from List<String>
我有方法 returns 我从 XML 文件中获取地图。我已经将该映射转换为将键和值分离到列表中。
但是我注意到值列表中有换行符。如何删除换行符并用 space 替换它们或将它们留空。
代码:
@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;
}
输出:
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]
如有任何帮助或帮助,我们将不胜感激。提前致谢
编辑:
XML 文件
<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>
设置value = xr.getText().trim()
。这将 trim 值开头和结尾的无关字符。
为了防止添加该值,请用 if (value != null && !value.isEmpty())
包裹 map.put(name, value)
您的代码正在提取元素名称和紧跟在开始元素之后的文本,忽略结束元素之后的任何文本。
因此,它收集:
Return = <newline><space><space>
ReturnData = <newline><space><space><space><space>
IRS1095A = <newline><space><space><space><space><space><space>
MonthlyPTCInformationGrpPP = <newline><space><space><space><space><space><space><space><space>
MonthlyPlanPremiumAmtPP = 136
...
然后将它们添加到 HashMap,它以随机顺序打乱 key/value 对,因此很难看出发生了什么。
已更新
我不会为你编写代码,但如果你想要 "value elements" 那么你需要:
- 看到时记住起始元素
- 收集任何文本,与其他已收集的文本连接起来,例如当您看到
- 当看到起始元素并记住起始元素时,验证文本是否为空或全部为空白,然后丢弃文本
- 看到结束元素时:
- 如果记住起始元素,将 elementName/text 添加到结果中,然后忘记起始元素并丢弃文本。注意:如果相同的元素名称可以多次出现,请不要使用 map。
- 如果不记得开始元素(忘记了),验证文本是否为空或全部为空白,然后丢弃文本
这将只收集叶元素,忽略任何 "layout"。
代码完全如上所述
好吧,我确实添加了丢失的资源清理。
Map<String, String> map = new HashMap<>();
try (FileInputStream in = new FileInputStream(file)) {
XMLStreamReader xr = XMLInputFactory.newInstance().createXMLStreamReader(in);
try (
String elementName = null;
StringBuilder textBuf = new StringBuilder();
while (xr.hasNext()) {
switch (xr.next()) {
case XMLStreamConstants.START_ELEMENT:
// 3. When seeing a start element and a start element is remembered
if (elementName != null) {
// verify text is empty or all whitespace
if (! textBuf.toString().trim().isEmpty())
throw new IllegalArgumentException("Found text mixed with elements");
// then discard text
textBuf.setLength(0);
}
// 1. Remember start element when seen
elementName = xr.getLocalName();
break;
case XMLStreamConstants.CHARACTERS:
case XMLStreamConstants.CDATA:
case XMLStreamConstants.SPACE:
// 2. Collect any text
textBuf.append(xr.getText());
break;
case XMLStreamConstants.END_ELEMENT: // 4. When seeing an end element
if (elementName != null) { // 1. if start element is remembered
// add elementName/text to result
map.put(elementName, textBuf.toString());
// then forget start element
elementName = null;
// and discard text
textBuf.setLength(0);
} else { // 2. if start element is not remembered (was forgotton)
// verify text is empty or all whitespace
if (! textBuf.toString().trim().isEmpty())
throw new IllegalArgumentException("Found text mixed with elements");
// then discard text
textBuf.setLength(0);
}
break;
default:
// ignore
}
}
} finally {
xr.close();
}
}
return map;
我有方法 returns 我从 XML 文件中获取地图。我已经将该映射转换为将键和值分离到列表中。
但是我注意到值列表中有换行符。如何删除换行符并用 space 替换它们或将它们留空。
代码:
@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;
}
输出:
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]
如有任何帮助或帮助,我们将不胜感激。提前致谢
编辑:
XML 文件
<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>
设置value = xr.getText().trim()
。这将 trim 值开头和结尾的无关字符。
为了防止添加该值,请用 if (value != null && !value.isEmpty())
map.put(name, value)
您的代码正在提取元素名称和紧跟在开始元素之后的文本,忽略结束元素之后的任何文本。
因此,它收集:
Return = <newline><space><space>
ReturnData = <newline><space><space><space><space>
IRS1095A = <newline><space><space><space><space><space><space>
MonthlyPTCInformationGrpPP = <newline><space><space><space><space><space><space><space><space>
MonthlyPlanPremiumAmtPP = 136
...
然后将它们添加到 HashMap,它以随机顺序打乱 key/value 对,因此很难看出发生了什么。
已更新
我不会为你编写代码,但如果你想要 "value elements" 那么你需要:
- 看到时记住起始元素
- 收集任何文本,与其他已收集的文本连接起来,例如当您看到
- 当看到起始元素并记住起始元素时,验证文本是否为空或全部为空白,然后丢弃文本
- 看到结束元素时:
- 如果记住起始元素,将 elementName/text 添加到结果中,然后忘记起始元素并丢弃文本。注意:如果相同的元素名称可以多次出现,请不要使用 map。
- 如果不记得开始元素(忘记了),验证文本是否为空或全部为空白,然后丢弃文本
这将只收集叶元素,忽略任何 "layout"。
代码完全如上所述
好吧,我确实添加了丢失的资源清理。
Map<String, String> map = new HashMap<>();
try (FileInputStream in = new FileInputStream(file)) {
XMLStreamReader xr = XMLInputFactory.newInstance().createXMLStreamReader(in);
try (
String elementName = null;
StringBuilder textBuf = new StringBuilder();
while (xr.hasNext()) {
switch (xr.next()) {
case XMLStreamConstants.START_ELEMENT:
// 3. When seeing a start element and a start element is remembered
if (elementName != null) {
// verify text is empty or all whitespace
if (! textBuf.toString().trim().isEmpty())
throw new IllegalArgumentException("Found text mixed with elements");
// then discard text
textBuf.setLength(0);
}
// 1. Remember start element when seen
elementName = xr.getLocalName();
break;
case XMLStreamConstants.CHARACTERS:
case XMLStreamConstants.CDATA:
case XMLStreamConstants.SPACE:
// 2. Collect any text
textBuf.append(xr.getText());
break;
case XMLStreamConstants.END_ELEMENT: // 4. When seeing an end element
if (elementName != null) { // 1. if start element is remembered
// add elementName/text to result
map.put(elementName, textBuf.toString());
// then forget start element
elementName = null;
// and discard text
textBuf.setLength(0);
} else { // 2. if start element is not remembered (was forgotton)
// verify text is empty or all whitespace
if (! textBuf.toString().trim().isEmpty())
throw new IllegalArgumentException("Found text mixed with elements");
// then discard text
textBuf.setLength(0);
}
break;
default:
// ignore
}
}
} finally {
xr.close();
}
}
return map;