本文整理汇总了Java中org.w3c.dom.ls.LSSerializer.writeToString方法的典型用法代码示例。如果您正苦于以下问题:Java LSSerializer.writeToString方法的具体用法?Java LSSerializer.writeToString怎么用?Java LSSerializer.writeToString使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.w3c.dom.ls.LSSerializer
的用法示例。
在下文中一共展示了LSSerializer.writeToString方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: validateXmlResult
import org.w3c.dom.ls.LSSerializer; //导入方法依赖的package包/类
private void validateXmlResult(Element resultXml) throws KalturaApiException {
Element resultElement = null;
try {
resultElement = XmlUtils.getElementByXPath(resultXml, "/xml/result");
} catch (XPathExpressionException xee) {
// AZ (unicon) - necessary in order to debug
String resultXmlStr;
try {
Document document = resultXml.getOwnerDocument();
DOMImplementationLS domImplLS = (DOMImplementationLS) document.getImplementation();
LSSerializer serializer = domImplLS.createLSSerializer();
resultXmlStr = serializer.writeToString(resultXml);
} catch (Exception e) {
resultXmlStr = "Unable to get XML result: "+e;
}
throw new KalturaApiException("XPath expression exception ("+xee+") evaluating result:"+resultXmlStr);
}
if (resultElement != null) {
return;
} else {
throw new KalturaApiException("Invalid result");
}
}
示例2: main
import org.w3c.dom.ls.LSSerializer; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.newDocument();
DOMImplementation impl = document.getImplementation();
DOMImplementationLS implLS = (DOMImplementationLS) impl.getFeature("LS", "3.0");
LSSerializer dsi = implLS.createLSSerializer();
/* We should have here incorrect document without getXmlVersion() method:
* Such Document is generated by replacing the JDK bootclasses with it's
* own Node,Document and DocumentImpl classes (see run.sh). According to
* XERCESJ-1007 the AbstractMethodError should be thrown in such case.
*/
String result = dsi.writeToString(document);
System.out.println("Result:" + result);
}
示例3: testLSInputParsingString
import org.w3c.dom.ls.LSSerializer; //导入方法依赖的package包/类
@Test
public void testLSInputParsingString() throws Exception {
DOMImplementationLS impl = (DOMImplementationLS) getDocumentBuilder().getDOMImplementation();
String xml = "<?xml version='1.0'?><test>runDocumentLS_Q6</test>";
LSParser domParser = impl.createLSParser(MODE_SYNCHRONOUS, null);
LSSerializer domSerializer = impl.createLSSerializer();
// turn off xml decl in serialized string for comparison
domSerializer.getDomConfig().setParameter("xml-declaration", Boolean.FALSE);
LSInput src = impl.createLSInput();
src.setStringData(xml);
assertEquals(src.getStringData(), xml);
Document doc = domParser.parse(src);
String result = domSerializer.writeToString(doc);
assertEquals(result, "<test>runDocumentLS_Q6</test>");
}
示例4: format
import org.w3c.dom.ls.LSSerializer; //导入方法依赖的package包/类
public static String format(String xml) {
try {
final InputSource src = new InputSource(new StringReader(xml));
final Node document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(src).getDocumentElement();
final Boolean keepDeclaration = Boolean.valueOf(xml.startsWith("<?xml"));
//May need this: System.setProperty(DOMImplementationRegistry.PROPERTY,"com.sun.org.apache.xerces.internal.dom.DOMImplementationSourceImpl");
final DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
final DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
final LSSerializer writer = impl.createLSSerializer();
writer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE); // Set this to true if the output needs to be beautified.
writer.getDomConfig().setParameter("xml-declaration", keepDeclaration); // Set this to true if the declaration is needed to be outputted.
return writer.writeToString(document);
} catch (Exception e) {
return xml;
}
}
示例5: main
import org.w3c.dom.ls.LSSerializer; //导入方法依赖的package包/类
public static void main(String[] args) throws Exception
{
ArrayList<LineItem> items = new ArrayList<>();
items.add(new LineItem(new Product("Toaster", 29.95), 3));
items.add(new LineItem(new Product("Hair dryer", 24.95), 1));
ItemListBuilder builder = new ItemListBuilder();
Document doc = builder.build(items);
DOMImplementation impl = doc.getImplementation();
DOMImplementationLS implLS
= (DOMImplementationLS) impl.getFeature("LS", "3.0");
LSSerializer ser = implLS.createLSSerializer();
String out = ser.writeToString(doc);
System.out.println(out);
}
示例6: load
import org.w3c.dom.ls.LSSerializer; //导入方法依赖的package包/类
public void load(File f) throws ConfigPersisterException {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(f);
DOMImplementationLS domImplementation = (DOMImplementationLS) doc.getImplementation();
LSSerializer lsSerializer = domImplementation.createLSSerializer();
String configString = lsSerializer.writeToString(doc);
_config = (Config) _xstream.fromXML(convertToCurrent(configString));
setConfigPath(f);
} catch (Exception e) {
throw new ConfigPersisterException(e);
}
}
示例7: formatHtml
import org.w3c.dom.ls.LSSerializer; //导入方法依赖的package包/类
protected String formatHtml(String html) throws MojoExecutionException {
try {
InputSource src = new InputSource(new StringReader(html));
Node document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(src).getDocumentElement();
Boolean keepDeclaration = Boolean.valueOf(html.startsWith("<?xml"));
DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
LSSerializer writer = impl.createLSSerializer();
writer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE);
writer.getDomConfig().setParameter("xml-declaration", keepDeclaration);
return writer.writeToString(document);
} catch (Exception e) {
throw new MojoExecutionException(e.getMessage(), e);
}
}
示例8: asString
import org.w3c.dom.ls.LSSerializer; //导入方法依赖的package包/类
/**
* Returns a textual representation of an XML Object.
*
* @param doc XML Dom Document
* @return String containing a textual representation of the object
*/
public static String asString(Document doc) {
try {
DOMImplementationLS domImplementation = (DOMImplementationLS) doc.getImplementation();
LSSerializer lsSerializer = domImplementation.createLSSerializer();
LSOutput lsOutput = domImplementation.createLSOutput();
lsOutput.setEncoding("UTF-8");
return lsSerializer.writeToString(doc);
} catch (Exception e) {
e.printStackTrace();
try {
DOMSource domSource = new DOMSource(doc);
StringWriter writer = new StringWriter();
StreamResult result = new StreamResult(writer);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer transformer = tf.newTransformer();
transformer.transform(domSource, result);
return writer.toString();
} catch(Exception ex) {
logger.error(ex);
return StackTrace.asString(ex);
}
}
}
示例9: indentXML
import org.w3c.dom.ls.LSSerializer; //导入方法依赖的package包/类
/**
* Method to generated idented XML
* Credit: Steve McLeod and DaoWen.
* Code from http://stackoverflow.com/a/11519668
* @param xml input xml in string format
* @return indented xml in string format
*/
public static String indentXML(String xml) {
try {
final InputSource src = new InputSource(new StringReader(xml));
final Node document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(src).getDocumentElement();
final Boolean keepDeclaration = xml.startsWith("<?xml");
//May need this: System.setProperty(DOMImplementationRegistry.PROPERTY,"com.sun.org.apache.xerces.internal.dom.DOMImplementationSourceImpl");
final DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
final DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
final LSSerializer writer = impl.createLSSerializer();
writer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE); // Set this to true if the output needs to be beautified.
writer.getDomConfig().setParameter("xml-declaration", keepDeclaration); // Set this to true if the declaration is needed to be outputted.
return writer.writeToString(document);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
示例10: nodeToString
import org.w3c.dom.ls.LSSerializer; //导入方法依赖的package包/类
protected static String nodeToString(Node n, boolean pretty) {
try {
final DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
final DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
final LSSerializer writer = impl.createLSSerializer();
writer.getDomConfig().setParameter("xml-declaration", false);
if (pretty) {
writer.getDomConfig().setParameter("format-pretty-print", true);
}
return writer.writeToString(n);
} catch (final Exception e) {
throw new RuntimeException(e);
}
}
示例11: serialize
import org.w3c.dom.ls.LSSerializer; //导入方法依赖的package包/类
protected String serialize(Node n)
throws ClassNotFoundException,
InstantiationException,
IllegalAccessException{
System.setProperty(DOMImplementationRegistry.PROPERTY,
"org.apache.xerces.dom.DOMImplementationSourceImpl");
DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
DOMImplementationLS impl =
(DOMImplementationLS)registry.getDOMImplementation("LS");
LSSerializer writer = impl.createLSSerializer();
String str = writer.writeToString(n);
// sometimes we get processor directives, such as <?xml version="1.0"?>
// strip these out
Pattern directivePattern = Pattern.compile("<\\?xml.*$", Pattern.MULTILINE);
Matcher m = directivePattern.matcher(str);
str = m.replaceFirst("");
return str;
}
示例12: normalizeXML
import org.w3c.dom.ls.LSSerializer; //导入方法依赖的package包/类
/**
* Normalize and pretty-print XML so that it can be compared using string
* compare. The following code does the following: - Removes comments -
* Makes sure attributes are ordered consistently - Trims every element -
* Pretty print the document
*
* @param xml The XML to be normalized
* @return The equivalent XML, but now normalized
*/
public static String normalizeXML(String xml) throws Exception {
// Remove all white space adjoining tags ("trim all elements")
xml = xml.replaceAll("\\s*<", "<");
xml = xml.replaceAll(">\\s*", ">");
DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
DOMImplementationLS domLS = (DOMImplementationLS) registry.getDOMImplementation("LS");
LSParser lsParser = domLS.createLSParser(DOMImplementationLS.MODE_SYNCHRONOUS, null);
LSInput input = domLS.createLSInput();
input.setStringData(xml);
Document document = lsParser.parse(input);
LSSerializer lsSerializer = domLS.createLSSerializer();
lsSerializer.getDomConfig().setParameter("comments", Boolean.FALSE);
lsSerializer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE);
return lsSerializer.writeToString(document);
}
示例13: toString
import org.w3c.dom.ls.LSSerializer; //导入方法依赖的package包/类
public static String toString( Element element )
{
DOMImplementation impl = element .getOwnerDocument() .getImplementation();
if ( impl .hasFeature( "LS", "3.0" ) ){
DOMImplementationLS lsImpl = (DOMImplementationLS) impl .getFeature("LS", "3.0");
LSSerializer serializer = lsImpl .createLSSerializer();
serializer .getDomConfig() .setParameter( "xml-declaration", false ); //by default its true, so set it to false to get String without xml-declaration
return serializer .writeToString( element );
}
else {
try {
Transformer transformer = TransformerFactory.newInstance().newTransformer();
StringWriter stringWriter = new StringWriter();
transformer.transform(new DOMSource(element), new StreamResult(stringWriter));
return stringWriter.toString();
} catch (TransformerException e) {
e.printStackTrace();
return "<unableToSerialize/>";
}
}
}
示例14: format
import org.w3c.dom.ls.LSSerializer; //导入方法依赖的package包/类
public static String format(String xml) {
try {
final InputSource src = new InputSource(new StringReader(xml));
final Node document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(src).getDocumentElement();
final Boolean keepDeclaration = Boolean.valueOf(xml.startsWith("<?xml"));
//May need this: System.setProperty(DOMImplementationRegistry.PROPERTY,"com.sun.org.apache.xerces.internal.dom.DOMImplementationSourceImpl");
final DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
final DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
final LSSerializer writer = impl.createLSSerializer();
writer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE); // Set this to true if the output needs to be beautified.
writer.getDomConfig().setParameter("xml-declaration", keepDeclaration); // Set this to true if the declaration is needed to be outputted.
return writer.writeToString(document);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
示例15: readTopics
import org.w3c.dom.ls.LSSerializer; //导入方法依赖的package包/类
public void readTopics() {
// extract XML topics from entire XML file
NodeList topicList = XMLTools.getNodes(new File(System.getProperty(ConfigConstants.TOPICS_FILE)), xTopic);
// loop over each topic and create topic objects
for (int i = 0; i < topicList.getLength(); i++) {
Node topicNode = topicList.item(i);
// convert back into String representation
DOMImplementationLS domImplLS = (DOMImplementationLS) topicNode.getOwnerDocument().getImplementation();
LSSerializer serializer = domImplLS.createLSSerializer();
String topicString = serializer.writeToString(topicNode);
// read single topic
CLEF2011Query topic = readTopic(topicString);
// store
this.topics.add(topic);
}
}