简体   繁体   English

我想将输出流转换为String对象

[英]I want to convert an output stream into String object

I want to convert an OutputStream into a String object. 我想将OutputStream转换为String对象。 I am having an OutputStream object returned after marshalling the JAXB object. 我在编组JAXB对象后返回了一个OutputStream对象。

not very familiar with jaxb, from what i was able to find you can convert into a string using 不是很熟悉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();
}

ws.apache.org ws.apache.org

but I'm not sure about a stirng object. 但我不确定一个搅拌物。 still searching. 仍在搜索中。

** EDIT **编辑

Marshalling a non-element 编组非元素

Another common use case is where you have an object that doesn't have @XmlRootElement on it. 另一个常见的用例是你有一个没有@XmlRootElement的对象。 JAXB allows you to marshal it like this: JAXB允许你像这样编组它:

marshaller.marshal( new JAXBElement( marshaller.marshal(新的JAXBElement(
new QName("","rootTag"),Point.class,new Point(...))); new QName(“”,“rootTag”),Point.class,new Point(...)));

This puts the element as the root element, followed by the contents of the object, then . 这将元素作为根元素,然后是对象的内容。 You can actually use it with a class that has @XmlRootElement, and that simply renames the root element name. 实际上,您可以将它与具有@XmlRootElement的类一起使用,并且只需重命名根元素名称。

At the first glance the second Point.class parameter may look redundant, but it's actually necessary to determine if the marshaller will produce (infamous) @xsi:type. 乍一看,第二个Point.class参数可能看起来多余,但实际上有必要确定编组器是否会产生(臭名昭着)@xsi:type。 In this example, both the class and the instance are Point, so you won't see @xsi:type. 在此示例中,类和实例都是Point,因此您将看不到@xsi:type。 But if they are different, you'll see it. 但如果它们不同,你会看到它。

This can be also used to marshal a simple object, like String or an integer. 这也可以用来编组一个简单的对象,比如String或整数。

marshaller.marshal( new JAXBElement( marshaller.marshal(新的JAXBElement(
new QName("","rootTag"),String.class,"foo bar")); new QName(“”,“rootTag”),String.class,“foo bar”));

But unfortunately it cannot be used to marshal objects like List or Map, as they aren't handled as the first-class citizen in the JAXB world. 但不幸的是,它不能用于编组像List或Map这样的对象,因为它们不是作为JAXB世界中的一等公民处理的。

found HERE 这里找到了

    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();
    }
}    

Method to handle the XML and convert it to a string. 处理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);     

Method within your class to get the XML and Marshall it Edit The above method will both output the marshalled xml and also the xml as a string. 你的类中的方法来获取XML和Marshall它编辑上面的方法将输出编组的xml和xml作为字符串。

Yes, there is a way to do it: just pass the String writer as input to it. 是的,有一种方法可以做到:只需将String writer作为输入传递给它。 So that it the xml created will be written to it 因此创建的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();
           }
       }
   }

如何转换 ArrayList<string> 至 ArrayList<object> using.collect(Collectors.toList() 在 Java stream<div id="text_translate"><p> 如何在 Java stream 中使用.collect(Collectors.toList()将ArrayList&lt;String&gt;转换为ArrayList&lt;Object&gt;</p><p> 之前我用过</p><pre>CommonList = detailUtils.SelectIDValueGetterObservable(getActivity()).stream().forEach(i -&gt; CommonList.add(new foo(i));`</pre><p> 但是我遇到了这些<strong>副作用</strong></p><p> <a href="https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html" rel="nofollow noreferrer">https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html</a></p><p> 这建议.collect(Collectors.toList()</p><p> 我们该怎么做</p></div></object></string> - How can I convert ArrayList<String> to ArrayList<Object> using .collect(Collectors.toList() in Java stream

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我想在此块中转换为Java 8流吗? - i want to convert to java 8 stream in this block? 要转换ArrayList <HashMap<String, Object> &gt;串 - Want to convert ArrayList<HashMap<String, Object>> to string 我想将 String 转换为 long - I want convert String to long 转换清单 <HashMap<String, Object> &gt;串流 - Convert List<HashMap<String, Object>> to stream 我想通过JAXB将带有java.time.Localdate(JSR-310)的Object转换为XML,但输出错误 - I want to convert an Object with a java.time.Localdate (JSR-310) to XML via JAXB but the output is wrong 如何转换 ArrayList<string> 至 ArrayList<object> using.collect(Collectors.toList() 在 Java stream<div id="text_translate"><p> 如何在 Java stream 中使用.collect(Collectors.toList()将ArrayList&lt;String&gt;转换为ArrayList&lt;Object&gt;</p><p> 之前我用过</p><pre>CommonList = detailUtils.SelectIDValueGetterObservable(getActivity()).stream().forEach(i -&gt; CommonList.add(new foo(i));`</pre><p> 但是我遇到了这些<strong>副作用</strong></p><p> <a href="https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html" rel="nofollow noreferrer">https://docs.oracle.com/javase/8/docs/api/java/util/stream/package-summary.html</a></p><p> 这建议.collect(Collectors.toList()</p><p> 我们该怎么做</p></div></object></string> - How can I convert ArrayList<String> to ArrayList<Object> using .collect(Collectors.toList() in Java stream 如何将字符串流转换为字符串流对? - How can I convert a Stream of Strings to Stream of String pairs? 我想将json对象中的xml字符串数据转换为java中的json - I want to convert xml string data inside json object to json in java 将对象转换为 sysout 输出中的字符串,java - convert object to String in sysout output, java 据我所知,showinputdialog 返回一个字符串类型的 output 但它给了我一个错误,它无法将 object 转换为字符串 - As far as I know showinputdialog returns an output of type string but it gives me an error that it cannot convert the object to a String
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM