當前位置: 首頁>>代碼示例>>Java>>正文


Java XMLSerializer類代碼示例

本文整理匯總了Java中com.sun.org.apache.xml.internal.serialize.XMLSerializer的典型用法代碼示例。如果您正苦於以下問題:Java XMLSerializer類的具體用法?Java XMLSerializer怎麽用?Java XMLSerializer使用的例子?那麽, 這裏精選的類代碼示例或許可以為您提供幫助。


XMLSerializer類屬於com.sun.org.apache.xml.internal.serialize包,在下文中一共展示了XMLSerializer類的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。

示例1: formatXml

import com.sun.org.apache.xml.internal.serialize.XMLSerializer; //導入依賴的package包/類
private String formatXml(String sourceXml) {
    try {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        InputSource is = new InputSource(new StringReader(sourceXml));
        Document d = db.parse(is);
        // -- Formating XML ----------------------------------------------
        OutputFormat format = new OutputFormat(d);
        format.setIndenting(true);
        format.setIndent(4);
        Writer out = new StringWriter();
        XMLSerializer s = new XMLSerializer(out, format);
        s.serialize(d);
        return out.toString();
    } catch (ParserConfigurationException | SAXException | IOException e) {
        logger.severe(e.getMessage());
    }
    return sourceXml;
}
 
開發者ID:vrsl,項目名稱:MDD-JPA,代碼行數:20,代碼來源:SchemaPanel.java

示例2: prettyPrint

import com.sun.org.apache.xml.internal.serialize.XMLSerializer; //導入依賴的package包/類
private static String prettyPrint(String xml, Boolean omitXmlDeclaration) throws IOException, SAXException, ParserConfigurationException {

        DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document doc = db.parse(new InputSource(new StringReader(xml)));

        OutputFormat format = new OutputFormat(doc);
        format.setIndenting(true);
        format.setIndent(2);
        format.setOmitXMLDeclaration(omitXmlDeclaration);
        format.setLineWidth(Integer.MAX_VALUE);
        Writer outxml = new StringWriter();
        XMLSerializer serializer = new XMLSerializer(outxml, format);
        serializer.serialize(doc);

        return outxml.toString();

    }
 
開發者ID:hashmapinc,項目名稱:witsml-client,代碼行數:18,代碼來源:MyWitsmlClient.java

示例3: toXML

import com.sun.org.apache.xml.internal.serialize.XMLSerializer; //導入依賴的package包/類
/**
 * 轉為xml串
 *
 * @param obj
 * @return
 */
public String toXML(Object obj) {
	String result = null;
	try {
		JAXBContext context = JAXBContext.newInstance(obj.getClass());
		Marshaller m = context.createMarshaller();
		m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
		m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
		m.setProperty(Marshaller.JAXB_FRAGMENT, true);// 去掉報文頭
		OutputStream os = new ByteArrayOutputStream();
		XMLSerializer serializer = getXMLSerializer(os);
		m.marshal(obj, serializer.asContentHandler());
		result = os.toString();
	} catch (Exception e) {
		e.printStackTrace();
	}
	logger.info("response text:" + result);
	return result;
}
 
開發者ID:funtl,項目名稱:framework,代碼行數:25,代碼來源:JaxbParser.java

示例4: formatXml

import com.sun.org.apache.xml.internal.serialize.XMLSerializer; //導入依賴的package包/類
/**
 * Format xml string.
 *
 * @param uglyXml the ugly xml
 * @return the string
 */
public static String formatXml(String uglyXml) {
	try {
		String xmlHeader= "<?xml version=\"1.0\" encoding=\"UTF-8\"?>";
		DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
		DocumentBuilder db = dbf.newDocumentBuilder();
		InputSource is = new InputSource(new StringReader(uglyXml));
		final Document document = db.parse(is);
		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().replace(xmlHeader,"");
	} catch (Exception e) {
		return "";
	}
}
 
開發者ID:thlws,項目名稱:payment-wechat,代碼行數:26,代碼來源:ThlwsBeanUtil.java

示例5: pretify

import com.sun.org.apache.xml.internal.serialize.XMLSerializer; //導入依賴的package包/類
public static String pretify(String unformattedXml) {
    try {
        final Document document = parseXmlFile(unformattedXml);

        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);
    }
}
 
開發者ID:SabreDevStudio,項目名稱:SACS-Java,代碼行數:18,代碼來源:XMLPrettifier.java

示例6: printTo

import com.sun.org.apache.xml.internal.serialize.XMLSerializer; //導入依賴的package包/類
@Override
public void printTo(Writer out) throws IOException {
    if (dom == null) {
        return;
    }

    StringWriter sw = new StringWriter();

    OutputFormat format = new OutputFormat(dom);
    format.setIndenting(true);

    XMLSerializer serializer = new XMLSerializer(sw, format);
    serializer.serialize(dom);

    out.write(sw.toString());
    out.flush();
}
 
開發者ID:testIT-LivingDoc,項目名稱:livingdoc-core,代碼行數:18,代碼來源:XmlReport.java

示例7: XmlEditsVisitor

import com.sun.org.apache.xml.internal.serialize.XMLSerializer; //導入依賴的package包/類
/**
 * Create a processor that writes to the file named and may or may not
 * also output to the screen, as specified.
 *
 * @param filename Name of file to write output to
 * @param printToScreen Mirror output to screen?
 */
public XmlEditsVisitor(OutputStream out)
    throws IOException {
  this.out = out;
  OutputFormat outFormat = new OutputFormat("XML", "UTF-8", true);
  outFormat.setIndenting(true);
  outFormat.setIndent(2);
  outFormat.setDoctype(null, null);
  XMLSerializer serializer = new XMLSerializer(out, outFormat);
  contentHandler = serializer.asContentHandler();
  try {
    contentHandler.startDocument();
    contentHandler.startElement("", "", "EDITS", new AttributesImpl());
  } catch (SAXException e) {
    throw new IOException("SAX error: " + e.getMessage());
  }
}
 
開發者ID:Nextzero,項目名稱:hadoop-2.6.0-cdh5.4.3,代碼行數:24,代碼來源:XmlEditsVisitor.java

示例8: openResultFile

import com.sun.org.apache.xml.internal.serialize.XMLSerializer; //導入依賴的package包/類
private void openResultFile(File outputFile) throws SAXException
{
   try
   {
      out = new FileOutputStream(outputFile);

      OutputFormat of = new OutputFormat("XML", FILE_ENCODING, true);
      of.setIndent(1);
      of.setIndenting(true);
      XMLSerializer serializer = new XMLSerializer(out, of);

      writer = serializer.asContentHandler();
      writer.startDocument();

      startElement(XML_ROOT, NO_ATTRIBS);
   }
   catch (IOException io)
   {
      logger.error(ERROR_WRITE_RESULTS, io);
      close();
   }
}
 
開發者ID:dheraclio,項目名稱:dependometer,代碼行數:23,代碼來源:XMLWriter.java

示例9: generateOutput

import com.sun.org.apache.xml.internal.serialize.XMLSerializer; //導入依賴的package包/類
@Override
public String generateOutput(DocumentRecord documentRecord) {
	if (Format.XML.equals(documentRecord.getFormat())) {
		DOMHandle handle = documentRecord.getContent(new DOMHandle());
		Document document = handle.get();

		OutputFormat format = new OutputFormat(handle.get());
		format.setOmitXMLDeclaration(omitXmlDeclaration);

		StringWriter writer = new StringWriter();
		try {
			new XMLSerializer(writer, format).serialize(document);
			return writer.toString();
		} catch (IOException e) {
			throw new RuntimeException("Unable to serialize XML document to string: " + e.getMessage(), e);
		}
	} else if (logger.isDebugEnabled()) {
		logger.debug(format("Document '%s' has a format of '%s', so will not attempt to remove the XML declaration from it",
			documentRecord.getUri(), documentRecord.getFormat().name()));
	}

	return documentRecord.getContent(new StringHandle()).get();
}
 
開發者ID:marklogic-community,項目名稱:ml-javaclient-util,代碼行數:24,代碼來源:XmlOutputListener.java

示例10: getPrettyXml

import com.sun.org.apache.xml.internal.serialize.XMLSerializer; //導入依賴的package包/類
public static String getPrettyXml(byte[] xml) {
    try {
        String unformattedXml = new String(xml);
        final Document document = parseXmlFile(unformattedXml);

        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);
    }
}
 
開發者ID:dimagi,項目名稱:commcare-j2me,代碼行數:19,代碼來源:XmlUtil.java

示例11: formatXml

import com.sun.org.apache.xml.internal.serialize.XMLSerializer; //導入依賴的package包/類
/**
 * Format the given string as xml content.
 * @param xml
 * @return
 */
public static String formatXml(String xml) {
	try {
		DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
		DocumentBuilder domBuilder = domFactory.newDocumentBuilder();
		InputSource is = new InputSource(new StringReader(xml));
		Document doc = domBuilder.parse(is);
		OutputFormat format = new OutputFormat(doc);
		format.setLineWidth(80);
		format.setIndent(2);
		format.setIndenting(true);
		StringWriter out = new StringWriter();
		XMLSerializer xmls = new XMLSerializer(out, format);
		xmls.serialize(doc);
		return out.toString();
	} catch (Exception e) {
		e.printStackTrace();
	}
	return xml;
}
 
開發者ID:wangqi,項目名稱:gameserver,代碼行數:25,代碼來源:XmlUtil.java

示例12: printTo

import com.sun.org.apache.xml.internal.serialize.XMLSerializer; //導入依賴的package包/類
public void printTo(Writer out)
		throws IOException
{
	if (dom == null)
	{
		return;
	}

	StringWriter sw = new StringWriter();

	OutputFormat format = new OutputFormat(dom);
	format.setIndenting(true);

	XMLSerializer serializer = new XMLSerializer(sw, format);
	serializer.serialize(dom);

	out.write(sw.toString());
	out.flush();
}
 
開發者ID:greenpeppersoftware,項目名稱:greenpepper3,代碼行數:20,代碼來源:XmlReport.java

示例13: nodeToString

import com.sun.org.apache.xml.internal.serialize.XMLSerializer; //導入依賴的package包/類
protected String nodeToString(Node node) {
    StringWriter stringOut = new StringWriter();

    OutputFormat format = new OutputFormat(Method.XML, null, false);
    format.setOmitXMLDeclaration(true);

    XMLSerializer serial = new XMLSerializer(stringOut, format);

    try {
        serial.serialize(node);
    } catch (IOException e) {
        throw new RuntimeException(e);
    }

    return stringOut.toString();
}
 
開發者ID:kuali,項目名稱:rice,代碼行數:17,代碼來源:CustomSchemaParser.java

示例14: toXML

import com.sun.org.apache.xml.internal.serialize.XMLSerializer; //導入依賴的package包/類
/**
 * 轉為xml串
 * @param obj
 * @return
 */
public String toXML(Object obj){
	String result = null;
	try {
		JAXBContext context = JAXBContext.newInstance(obj.getClass());
		Marshaller m = context.createMarshaller();
		m.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
		m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
		m.setProperty(Marshaller.JAXB_FRAGMENT, true);// 去掉報文頭
	    OutputStream os = new ByteOutputStream();
		StringWriter writer = new StringWriter();
		XMLSerializer serializer = getXMLSerializer(os);
		m.marshal(obj, serializer.asContentHandler());
		result = os.toString();
	} catch (Exception e) {
		e.printStackTrace();
	}
	logger.info("response text:" + result);
	return result;
}
 
開發者ID:sword-org,項目名稱:sword-lang,代碼行數:25,代碼來源:JaxbParser.java

示例15: toString

import com.sun.org.apache.xml.internal.serialize.XMLSerializer; //導入依賴的package包/類
public String toString(){
	Document document = toDoc();
	
	OutputFormat format = new OutputFormat(document);

	StringWriter stringOut = new StringWriter();
	XMLSerializer serial2 = new XMLSerializer(stringOut, format);
	try {
		serial2.asDOMSerializer();
		serial2.serialize(document);
	} catch (IOException e) {
		e.printStackTrace();
	}
	
	String serialized = stringOut.toString();
	//TODO this isn't nice at all!
	serialized = serialized.replace("<?xml version=\"1.0\" encoding=\"UTF-8\"?>", "");
	serialized = serialized.replace("<root>", "");
	serialized = serialized.replace("</root>", "");

	return serialized;
}
 
開發者ID:iotsap,項目名稱:FiWare-Template-Handler,代碼行數:23,代碼來源:JsonErdfTransformation.java


注:本文中的com.sun.org.apache.xml.internal.serialize.XMLSerializer類示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。