/// <summary> /// Creates a DOM Document from an ISource. /// </summary> /// <remarks> /// <para> /// since XMLUnit 2.6.0 /// </para> /// </remarks> public static XmlDocument ToDocument(ISource s, bool prohibitDTD, XmlResolver resolver) { if (s == null) { throw new ArgumentNullException("s"); } DOMSource ds = s as DOMSource; if (ds != null) { XmlDocument doc = ds.Node as XmlDocument; if (doc != null) { return(doc); } } XmlDocument d = new XmlDocument(); d.XmlResolver = resolver; XmlReaderSettings sett = new XmlReaderSettings(); sett.ProhibitDtd = prohibitDTD; d.Load(XmlReader.Create(s.Reader, sett)); return(d); }
/// <summary>test xml generation</summary> /// <exception cref="Javax.Xml.Parsers.ParserConfigurationException"/> /// <exception cref="System.Exception"></exception> public virtual void TestQueueConfigurationParser() { JobQueueInfo info = new JobQueueInfo("root", "rootInfo"); JobQueueInfo infoChild1 = new JobQueueInfo("child1", "child1Info"); JobQueueInfo infoChild2 = new JobQueueInfo("child2", "child1Info"); info.AddChild(infoChild1); info.AddChild(infoChild2); DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.NewInstance(); DocumentBuilder builder = docBuilderFactory.NewDocumentBuilder(); Document document = builder.NewDocument(); // test QueueConfigurationParser.getQueueElement Element e = QueueConfigurationParser.GetQueueElement(document, info); // transform result to string for check DOMSource domSource = new DOMSource(e); StringWriter writer = new StringWriter(); StreamResult result = new StreamResult(writer); TransformerFactory tf = TransformerFactory.NewInstance(); Transformer transformer = tf.NewTransformer(); transformer.Transform(domSource, result); string str = writer.ToString(); NUnit.Framework.Assert.IsTrue(str.EndsWith("<queue><name>root</name><properties/><state>running</state><queue><name>child1</name><properties/><state>running</state></queue><queue><name>child2</name><properties/><state>running</state></queue></queue>" )); }
public void InternalFixBeforeAdobt() { // why does this fix our attribute value problem?? // <output id="Content"><div><a href=" ">sdfsdfsdf</a></div></output> // X:\jsc.svn\examples\php\PHPWiki\PHPWiki\ApplicationWebService.cs try { this.InternalEnsureElement(); var s = new DOMSource(this.InternalValue); var w = new StringWriter(); var r = new StreamResult(w); var f = TransformerFactory.newInstance(); f.newTransformer().transform(s, r); } catch { // X:\jsc.svn\examples\javascript\forms\FormsNIC\FormsNIC\ApplicationWebService.cs //Caused by: java.lang.NullPointerException // at org.apache.xml.serializer.ToStream.writeAttrString(ToStream.java:2099) // at org.apache.xml.serializer.ToStream.processAttributes(ToStream.java:2079) // at org.apache.xml.serializer.ToStream.closeStartTag(ToStream.java:2623) // at org.apache.xml.serializer.ToStream.characters(ToStream.java:1410) // at org.apache.xalan.transformer.TransformerIdentityImpl.characters(TransformerIdentityImpl.java:1126) // at org.apache.xml.serializer.TreeWalker.dispatachChars(TreeWalker.java:246) // at org.apache.xml.serializer.TreeWalker.startNode(TreeWalker.java:416) // at org.apache.xml.serializer.TreeWalker.traverse(TreeWalker.java:145) // at org.apache.xalan.transformer.TransformerIdentityImpl.transform(TransformerIdentityImpl.java:390) // at ScriptCoreLibJava.BCLImplementation.System.Xml.Linq.__XNode.InternalFixBeforeAdobt(__XNode.java:77) // ... 25 more throw; } }
public static void PrintNode(OutputStream @out, INode node, bool prettyPrint, bool includeXmlDeclaration) { try { Transformer serializer = tFactory.NewTransformer(); if (prettyPrint) { //Setup indenting to "pretty print" serializer.SetOutputProperty(OutputKeys.Indent, "yes"); serializer.SetOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2"); } if (!includeXmlDeclaration) { serializer.SetOutputProperty(OutputKeys.OmitXmlDeclaration, "yes"); } DOMSource xmlSource = new DOMSource(node); StreamResult outputTarget = new StreamResult(@out); serializer.Transform(xmlSource, outputTarget); } catch (TransformerException e) { throw new Exception(e); } }
/// <summary> /// Creates a DOM Node from an ISource. /// </summary> /// <remarks> /// Unless the source is a DOMSource this will return the same /// result as ToDocument. /// </remarks> public static XmlNode ToNode(this ISource s) { DOMSource ds = s as DOMSource; return(ds != null ? ds.Node : ToDocument(s)); }
public string InternalToString() { // http://faq.javaranch.com/java/DocumentToString var value = default(string); //Console.WriteLine("InternalToString " + new { this.InternalValue }); try { //I/System.Console(12089): Caused by: java.lang.NullPointerException //I/System.Console(12089): at org.apache.xml.serializer.ToStream.writeAttrString(ToStream.java:2099) //I/System.Console(12089): at org.apache.xml.serializer.ToStream.processAttributes(ToStream.java:2079) //I/System.Console(12089): at org.apache.xml.serializer.ToStream.closeStartTag(ToStream.java:2623) //I/System.Console(12089): at org.apache.xml.serializer.ToStream.startElement(ToStream.java:1927) //I/System.Console(12089): at org.apache.xalan.transformer.TransformerIdentityImpl.startElement(TransformerIdentityImpl.java:1073) //I/System.Console(12089): at org.apache.xml.serializer.TreeWalker.startNode(TreeWalker.java:359) //I/System.Console(12089): at org.apache.xml.serializer.TreeWalker.traverse(TreeWalker.java:145) //I/System.Console(12089): at org.apache.xalan.transformer.TransformerIdentityImpl.transform(TransformerIdentityImpl.java:390) //I/System.Console(12089): at ScriptCoreLibJava.BCLImplementation.System.Xml.Linq.__XNode.InternalToString(__XNode.java:81) this.InternalEnsureElement(); // http://stackoverflow.com/questions/9150403/how-do-you-debug-an-xml-object-that-causes-a-transform-error-when-writing-to-str // http://dotcommers.wordpress.com/2008/10/22/javaxxmltransformtransformerexception-javalangnullpointerexception-how-to-solve/ var s = new DOMSource(this.InternalValue); var w = new StringWriter(); var r = new StreamResult(w); var f = TransformerFactory.newInstance(); var transformer = f.newTransformer(); // http://stackoverflow.com/questions/15592025/transformer-setoutputpropertyoutputkeys-encoding-utf-8-is-not-working transformer.setOutputProperty(OutputKeys.METHOD, "xml"); // meses up fields as cookie headers? //transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(s, r); value = w.getBuffer().toString(); #region __XDocument var IsDocument = (this is __XDocument); if (!IsDocument) { // hack. { var prefix = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; if (value.StartsWith(prefix)) { value = value.Substring(prefix.Length); } } { var prefix = "\r\n"; if (value.StartsWith(prefix)) { value = value.Substring(prefix.Length); } } } #endregion } catch (Exception err) { // Message = java.lang.RuntimeException: Namespace for prefix 'xsi' has not been declared., StackTrace = jav // The input node can not be null for a DOMSource for newTemplates! // Z:\jsc.svn\examples\java\hybrid\JVMCLRWSDLMID\Program.cs //throw; value = err.Message; } return(value); }
public string InternalToString() { // http://faq.javaranch.com/java/DocumentToString var value = default(string); //Console.WriteLine("InternalToString " + new { this.InternalValue }); try { //I/System.Console(12089): Caused by: java.lang.NullPointerException //I/System.Console(12089): at org.apache.xml.serializer.ToStream.writeAttrString(ToStream.java:2099) //I/System.Console(12089): at org.apache.xml.serializer.ToStream.processAttributes(ToStream.java:2079) //I/System.Console(12089): at org.apache.xml.serializer.ToStream.closeStartTag(ToStream.java:2623) //I/System.Console(12089): at org.apache.xml.serializer.ToStream.startElement(ToStream.java:1927) //I/System.Console(12089): at org.apache.xalan.transformer.TransformerIdentityImpl.startElement(TransformerIdentityImpl.java:1073) //I/System.Console(12089): at org.apache.xml.serializer.TreeWalker.startNode(TreeWalker.java:359) //I/System.Console(12089): at org.apache.xml.serializer.TreeWalker.traverse(TreeWalker.java:145) //I/System.Console(12089): at org.apache.xalan.transformer.TransformerIdentityImpl.transform(TransformerIdentityImpl.java:390) //I/System.Console(12089): at ScriptCoreLibJava.BCLImplementation.System.Xml.Linq.__XNode.InternalToString(__XNode.java:81) this.InternalEnsureElement(); // http://stackoverflow.com/questions/9150403/how-do-you-debug-an-xml-object-that-causes-a-transform-error-when-writing-to-str // http://dotcommers.wordpress.com/2008/10/22/javaxxmltransformtransformerexception-javalangnullpointerexception-how-to-solve/ var s = new DOMSource(this.InternalValue); var w = new StringWriter(); var r = new StreamResult(w); var f = TransformerFactory.newInstance(); f.newTransformer().transform(s, r); value = w.getBuffer().toString(); #region __XDocument var IsDocument = (this is __XDocument); if (!IsDocument) { // hack. { var prefix = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"; if (value.StartsWith(prefix)) { value = value.Substring(prefix.Length); } } { var prefix = "\r\n"; if (value.StartsWith(prefix)) { value = value.Substring(prefix.Length); } } } #endregion } catch { // The input node can not be null for a DOMSource for newTemplates! throw; } return(value); }