本文整理汇总了Java中org.apache.xml.serialize.XMLSerializer.serialize方法的典型用法代码示例。如果您正苦于以下问题:Java XMLSerializer.serialize方法的具体用法?Java XMLSerializer.serialize怎么用?Java XMLSerializer.serialize使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.xml.serialize.XMLSerializer
的用法示例。
在下文中一共展示了XMLSerializer.serialize方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: save
import org.apache.xml.serialize.XMLSerializer; //导入方法依赖的package包/类
public void save() throws AtsConfigurationException {
// save the XML file
try {
OutputFormat format = new OutputFormat(doc);
format.setIndenting(true);
format.setIndent(4);
format.setLineWidth(1000);
XMLSerializer serializer = new XMLSerializer(new FileOutputStream(new File(atsConfigurationFile)),
format);
serializer.serialize(doc);
} catch (Exception e) {
throw new AtsConfigurationException("Error saving ATS configuration in '" + atsConfigurationFile
+ "'", e);
}
}
示例2: formatDocumentForTesting
import org.apache.xml.serialize.XMLSerializer; //导入方法依赖的package包/类
/**
* Cette méthode permet de reformater le contenu d'une chaîne XML
* et donc de s'affranchir des problèmes liés au formatage (tabulations, espaces, retours chariot, ...).
*
* @param document
* @return une chaîne de caractères "normalisée" créé à partir d'un document Document Object Model
*/
public String formatDocumentForTesting(Document document) {
try {
OutputFormat format = new OutputFormat(document);
format.setLineWidth(65);
format.setIndenting(true);
format.setIndent(2);
Writer out = new StringWriter();
XMLSerializer serializer = new XMLSerializer(out, format);
serializer.serialize(document);
return out.toString();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
示例3: writeXMLFile
import org.apache.xml.serialize.XMLSerializer; //导入方法依赖的package包/类
public void writeXMLFile(String out_fName){
try{
BufferedWriter out= new BufferedWriter(new FileWriter(out_fName));
StringWriter stringOut = new StringWriter(); //Writer will be a String
OutputFormat format = new OutputFormat(rootDoc); //Serialize DOM
XMLSerializer serial = new XMLSerializer(stringOut, format );
serial.asDOMSerializer(); // As a DOM Serializer
serial.serialize( rootDoc.getDocumentElement() );
out.write(stringOut.toString() ); //Spit out DOM as a String
out.close();
} catch ( Exception ex ) {
ex.printStackTrace();
}
}
示例4: store
import org.apache.xml.serialize.XMLSerializer; //导入方法依赖的package包/类
/**
* store loaded data to xml file
* @throws SearchException
*/
protected final synchronized void store() throws SearchException {
//Collection.Key[] keys=collections.keys();
Iterator<Key> it = collections.keyIterator();
Key k;
while(it.hasNext()) {
k=it.next();
Element collEl = getCollectionElement(k.getString());
SearchCollection sc = getCollectionByName(k.getString());
setAttributes(collEl,sc);
}
OutputFormat format = new OutputFormat(doc, null, true);
format.setLineSeparator("\r\n");
format.setLineWidth(72);
OutputStream os=null;
try {
XMLSerializer serializer = new XMLSerializer(os=IOUtil.toBufferedOutputStream(searchFile.getOutputStream()), format);
serializer.serialize(doc.getDocumentElement());
} catch (IOException e) {
throw new SearchException(e);
}
finally {
IOUtil.closeEL(os);
}
}
示例5: validate
import org.apache.xml.serialize.XMLSerializer; //导入方法依赖的package包/类
public static void validate(
Document doc,
String schemaLocationPropertyValue,
EntityResolver resolver)
throws IOException,
SAXException
{
OutputFormat format = new OutputFormat(doc, null, true);
StringWriter writer = new StringWriter(1000);
XMLSerializer serial = new XMLSerializer(writer, format);
serial.asDOMSerializer();
serial.serialize(doc);
String docString = writer.toString();
validate(docString, schemaLocationPropertyValue, resolver);
}
示例6: save
import org.apache.xml.serialize.XMLSerializer; //导入方法依赖的package包/类
public static void save(Writer writer, Document document)
throws IOException
{
OutputFormat outputFormat = new OutputFormat(document);
outputFormat.setIndenting(true);
outputFormat.setLineWidth(Integer.MAX_VALUE);
outputFormat.setLineSeparator(Util.nl);
try {
XMLSerializer serializer = new XMLSerializer(writer, outputFormat);
serializer.serialize(document);
} finally {
if (writer != null) {
writer.close();
}
}
}
示例7: printToFile
import org.apache.xml.serialize.XMLSerializer; //导入方法依赖的package包/类
/**
* This method uses Xerces specific classes
* prints the XML document to file.
*/
private void printToFile(){
try
{
//print
OutputFormat format = new OutputFormat(dom);
format.setIndenting(true);
//to generate output to console use this serializer
//XMLSerializer serializer = new XMLSerializer(System.out, format);
//to generate a file output use fileoutputstream instead of system.out
XMLSerializer serializer = new XMLSerializer(
new FileOutputStream(new File("book.xml")), format);
serializer.serialize(dom);
} catch(IOException ie) {
ie.printStackTrace();
}
}
示例8: addXMLNameSpace
import org.apache.xml.serialize.XMLSerializer; //导入方法依赖的package包/类
private byte[] addXMLNameSpace(Document xmlDoc,
String nameSpace) {
Node node = xmlDoc.getDocumentElement();
Element element = (Element) node;
element.setAttribute("xmlns", nameSpace);
OutputFormat outputFormat = new OutputFormat(xmlDoc);
outputFormat.setOmitDocumentType(true);
ByteArrayOutputStream out = new ByteArrayOutputStream();
XMLSerializer serializer = new XMLSerializer(out, outputFormat);
try {
serializer.asDOMSerializer();
serializer.serialize(xmlDoc.getDocumentElement());
} catch (IOException e) {
throw new RuntimeException(e);
}
return out.toByteArray();
}
示例9: formatXml
import org.apache.xml.serialize.XMLSerializer; //导入方法依赖的package包/类
/**
* This function will format the XML profile with intends and new lines.
*
* @param xmlToFormat the xml String you want to format
* @return the formated version of teh given XML string
*/
private String formatXml(String xmlToFormat) {
try {
final Document document = generateXmlDocument(xmlToFormat);
OutputFormat format = new OutputFormat(document);
format.setLineWidth(65);
format.setIndenting(true);
format.setIndent(2);
format.setOmitXMLDeclaration(true);
Writer out = new StringWriter();
XMLSerializer serializer = new XMLSerializer(out, format);
serializer.serialize(document);
return out.toString();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
示例10: addXMLNameSpace
import org.apache.xml.serialize.XMLSerializer; //导入方法依赖的package包/类
private byte[] addXMLNameSpace(Document xmlDoc,
String nameSpace){
Node node = xmlDoc.getDocumentElement();
Element element = (Element)node;
element.setAttribute("xmlns", nameSpace);
OutputFormat outputFormat = new OutputFormat(xmlDoc);
outputFormat.setOmitDocumentType(true);
ByteArrayOutputStream out = new ByteArrayOutputStream();
XMLSerializer serializer = new XMLSerializer( out,outputFormat );
try {
serializer.asDOMSerializer();
serializer.serialize( xmlDoc.getDocumentElement());
}
catch (IOException e) {
throw new RuntimeException(e);
}
return out.toByteArray();
}
示例11: getResponseFromFile
import org.apache.xml.serialize.XMLSerializer; //导入方法依赖的package包/类
private String getResponseFromFile(String file) throws Exception {
String filePath = this.getClass().getResource(file).getPath();
Document xmlDocument;
InputStream inputXML;
inputXML = new FileInputStream(new File(filePath));
DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = builderFactory.newDocumentBuilder();
xmlDocument = builder.parse(inputXML);
OutputFormat format = new OutputFormat(xmlDocument);
format.setLineWidth(65);
format.setIndenting(true);
format.setIndent(2);
Writer out = new StringWriter();
XMLSerializer serializer = new XMLSerializer(out, format);
serializer.serialize(xmlDocument);
return out.toString();
}
示例12: getDocument
import org.apache.xml.serialize.XMLSerializer; //导入方法依赖的package包/类
public String getDocument() {
OutputFormat of = new OutputFormat("XML", "ISO-8859-1", true);
of.setIndent(1);
of.setIndenting(true);
// of.setDoctype(null, "users.dtd");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos);
XMLSerializer serializer = new XMLSerializer(ps, of);
try {
serializer.asDOMSerializer();
serializer.serialize(doc.getDocumentElement());
ps.close();
return baos.toString();
}
catch (IOException e) {
return null;
}
}
示例13: printToFile
import org.apache.xml.serialize.XMLSerializer; //导入方法依赖的package包/类
/**
* This method uses Xerces specific classes
* prints the XML document to file.
*/
private void printToFile(Document dom, File file){
try
{
OutputFormat format = new OutputFormat(dom);
format.setIndenting(true);
//to generate output to console use this serializer
// XMLSerializer serializer = new XMLSerializer(System.out, format);
//to generate a file output use fileoutputstream instead of system.out
XMLSerializer serializer = new XMLSerializer(
new FileOutputStream(file), format);
serializer.serialize(dom);
} catch(IOException ie) {
ie.printStackTrace();
}
}
示例14: extractNoteContent
import org.apache.xml.serialize.XMLSerializer; //导入方法依赖的package包/类
/**
* Extracts the node content. Basically returns every character in the
* subsection element.
*
* @param subsection
* the subsection of a rule
* @return the node content
* @throws ParserConfigurationException
* in case of an error
* @throws IOException
* in case of an error
*/
protected String extractNoteContent(final Element subsection) throws ParserConfigurationException,
IOException {
StringWriter writer = new StringWriter();
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.newDocument();
OutputFormat format = new OutputFormat(doc);
format.setOmitXMLDeclaration(true);
XMLSerializer serializer = new XMLSerializer(writer, format);
serializer.serialize(subsection);
String serialized = writer.getBuffer().toString();
serialized = StringUtils.substringAfter(serialized, ">");
return StringUtils.substringBeforeLast(serialized, "<");
}
示例15: getXml
import org.apache.xml.serialize.XMLSerializer; //导入方法依赖的package包/类
@Override
public String getXml() throws IOException {
try {
ByteArrayOutputStream bos = new ByteArrayOutputStream();
OutputFormat format = new OutputFormat();
format.setLineWidth(200);
format.setIndenting(true);
format.setIndent(2);
XMLSerializer serializer = new XMLSerializer(bos, format);
serializer.serialize(((Document)this.node).getDocumentElement());
return new String(bos.toByteArray(), "UTF-8");
} catch(Exception e) {
e.printStackTrace();
}
return null;
}