[英]I want to convert an output stream into String object
我想將OutputStream
轉換為String
對象。 我在編組JAXB對象后返回了一個OutputStream
對象。
不是很熟悉jaxb,從我能夠找到你可以轉換成字符串使用
public String asString(JAXBContext pContext,
Object pObject)
throws
JAXBException {
java.io.StringWriter sw = new StringWriter();
Marshaller marshaller = pContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
marshaller.marshal(pObject, sw);
return sw.toString();
}
但我不確定一個攪拌物。 仍在搜索中。
**編輯
編組非元素
另一個常見的用例是你有一個沒有@XmlRootElement的對象。 JAXB允許你像這樣編組它:
marshaller.marshal(新的JAXBElement(
new QName(“”,“rootTag”),Point.class,new Point(...)));這將元素作為根元素,然后是對象的內容。 實際上,您可以將它與具有@XmlRootElement的類一起使用,並且只需重命名根元素名稱。
乍一看,第二個Point.class參數可能看起來多余,但實際上有必要確定編組器是否會產生(臭名昭着)@xsi:type。 在此示例中,類和實例都是Point,因此您將看不到@xsi:type。 但如果它們不同,你會看到它。
這也可以用來編組一個簡單的對象,比如String或整數。
marshaller.marshal(新的JAXBElement(
new QName(“”,“rootTag”),String.class,“foo bar”));但不幸的是,它不能用於編組像List或Map這樣的對象,因為它們不是作為JAXB世界中的一等公民處理的。
在這里找到了
StringWriter sw = new StringWriter();
com.integra.xml.Integracao integracao = new Integracao();
integracao.add(...);
try {
JAXBContext context = JAXBContext.newInstance("com.integra.xml");
Marshaller marshaller = context.createMarshaller();
marshaller.marshal(integracao, sw );
System.out.println(sw.toString());
} catch (JAXBException e) {
e.printStackTrace();
}
public String readFile(String pathname) throws IOException {
File file = new File(pathname);
StringBuilder fileContents = new StringBuilder((int) file.length());
Scanner scanner = new Scanner(file);
String lineSeparator = System.getProperty("line.separator");
try {
while (scanner.hasNextLine()) {
fileContents.append(scanner.nextLine() + lineSeparator);
}
return fileContents.toString();
}
finally {
scanner.close();
}
}
處理XML並將其轉換為字符串的方法。
JAXBContext jc = JAXBContext.newInstance(ClassMatchingStartofXMLTags.class);
Unmarshaller unmarshaller = jc.createUnmarshaller();
//store filepath to be used
String filePath = "YourXMLFile.xml";
File xmlFile = new File(filePath);
//Set up xml Marshaller
ClassMatchingStartofXMLTags xmlMarshaller = (ClassMatchingStartofXMLTags) unmarshaller.unmarshal(xmlFileEdit);
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
// Use marshall to output the contents of the xml file
System.out.println("Marshalled XML\n");
marshaller.marshal(xmlMarshaller, System.out);
//Use Readfile method to convert the XML to a string and output the results
System.out.println("\nXML to String");
String strFile = ReadFile(xmlFile)
System.out.println(strFile);
你的類中的方法來獲取XML和Marshall它編輯上面的方法將輸出編組的xml和xml作為字符串。
是的,有一種方法可以做到:只需將String writer作為輸入傳遞給它。 因此創建的xml將被寫入它
public void saveSettings() throws IOException {
FileOutputStream os = null;
//Declare a StringWriter to which the output has to go
StringWriter sw = new StringWriter();
try {
Answer ans1=new Answer(101,"java is a programming language","ravi");
Answer ans2=new Answer(102,"java is a platform","john");
ArrayList<Answer> list=new ArrayList<Answer>();
list.add(ans1);
list.add(ans2);
settings=new Question(1,"What is java?",list);
os = new FileOutputStream(FILE_NAME);
this.marshaller.marshal(settings, new StreamResult(sw));
System.out.println(sw.toString());
new File(FILE_NAME).delete();
} finally {
if (os != null) {
os.close();
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.