无法使用 JAXB 正确解组 XML 包含选项卡的文件?
Not able to unmarshall XML file that contains tabs with JAXB correctly?
我有一个 xml 文件如下:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<billFile fileName=".dat" hasHeader="false" fieldDelimiter=" "/>
fieldDelimiter 包含一个制表符。当我调用 Unmarshaller.unmarshal 将此 xml 文件转换为对象时,选项卡被 space 替换。我希望它保留为一个选项卡。这是我的 java 代码:
import java.io.StringReader;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.stream.StreamSource;
public class XMLToConfig
{
public static void main(String[] args) throws JAXBException
{
String input = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?> <billFile fileName=\".dat\" hasHeader=\"false\" fieldDelimiter=\" \"/>";
JAXBContext jaxbContext = JAXBContext.newInstance(BillFile.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
BillFile bf = (BillFile)jaxbUnmarshaller.unmarshal(new StreamSource(new StringReader(input), "UTF8"));
}
}
您必须使用制表符的 Unicode 而不是真正的制表符:
unicode for tab and sapce
我有一个 xml 文件如下:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<billFile fileName=".dat" hasHeader="false" fieldDelimiter=" "/>
fieldDelimiter 包含一个制表符。当我调用 Unmarshaller.unmarshal 将此 xml 文件转换为对象时,选项卡被 space 替换。我希望它保留为一个选项卡。这是我的 java 代码:
import java.io.StringReader;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.transform.stream.StreamSource;
public class XMLToConfig
{
public static void main(String[] args) throws JAXBException
{
String input = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?> <billFile fileName=\".dat\" hasHeader=\"false\" fieldDelimiter=\" \"/>";
JAXBContext jaxbContext = JAXBContext.newInstance(BillFile.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
BillFile bf = (BillFile)jaxbUnmarshaller.unmarshal(new StreamSource(new StringReader(input), "UTF8"));
}
}
您必须使用制表符的 Unicode 而不是真正的制表符:
unicode for tab and sapce