Guest User

Untitled

a guest
Jul 19th, 2018
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.00 KB | None | 0 0
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <Payment xmlns="http://api.com/schema/store/1.0">
  3. <Store>abc</Store>
  4. </Payment>
  5.  
  6. <?xml version="1.0" encoding="UTF-8"?>
  7. <Payment>
  8. <Store>abc</Store>
  9. </Payment>
  10.  
  11. public class XPathUtils {
  12.  
  13. public static void main(String[] args) {
  14. String xml = "<?xml version="1.0" encoding="UTF-8"?><Payment xmlns="http://api.com/schema/store/1.0"><Store>abc</Store></Payment>";
  15. String afterNsRemoval = removeNameSpace(xml);
  16. System.out.println("afterNsRemoval = " + afterNsRemoval);
  17. }
  18.  
  19. public static String removeNameSpace(String xml) {
  20. try {
  21. System.out.println("before xml = " + xml);
  22. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  23. DocumentBuilder builder = factory.newDocumentBuilder();
  24. InputSource inputSource = new InputSource(new StringReader(xml));
  25. Document xmlDoc = builder.parse(inputSource);
  26. Node root = xmlDoc.getDocumentElement();
  27. NodeList rootchildren = root.getChildNodes();
  28. Element newroot = xmlDoc.createElement(root.getNodeName());
  29. for (int i = 0; i < rootchildren.getLength(); i++) {
  30. newroot.appendChild(rootchildren.item(i).cloneNode(true));
  31. }
  32. xmlDoc.replaceChild(newroot, root);
  33. return xmlDoc.toString();
  34. } catch (Exception e) {
  35. System.out.println("Could not parse message as xml: " + e.getMessage());
  36. }
  37. return "";
  38. }
  39. }
  40.  
  41. before xml = <?xml version="1.0" encoding="UTF-8"?><Payment xmlns="http://api.com/schema/store/1.0"><Store>abc</Store></Payment>
  42.  
  43. afterNsRemoval = [#document: null]
  44.  
  45. import com.ximpleware.*;
  46. import java.io.*;
  47.  
  48. public class removeAttrNode {
  49. public static void main(String[] s) throws VTDException, Exception{
  50. VTDGen vg = new VTDGen(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
  51. String xml = "<?xml version="1.0" encoding="UTF-8"?><Payment xmlns="http://api.com/schema/store/1.0"><Store>abc</Store></Payment>";
  52. vg.setDoc(xml.getBytes());
  53. vg.parse(false); // turn off namespace awareness so that
  54. VTDNav vn = vg.getNav();
  55. AutoPilot ap = new AutoPilot(vn);
  56. XMLModifier xm = new XMLModifier(vn);
  57. ap.selectXPath("//@xmlns");
  58. int i=0;
  59. while((i=ap.evalXPath())!=-1){
  60. xm.remove();
  61. }
  62. xm.output(baos);
  63. System.out.println(baos.toString());
  64. }
  65. }
  66.  
  67. public static String removeNameSpace(String xml) {
  68.  
  69.  
  70. try {
  71. TransformerFactory tf = TransformerFactory.newInstance();
  72. Transformer transformer = tf.newTransformer();
  73. transformer.setOutputProperty( OutputKeys.METHOD, "xml" );
  74. transformer.setOutputProperty( OutputKeys.INDENT, "false" );
  75. System.out.println("before xml = " + xml);
  76. DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
  77. DocumentBuilder builder = factory.newDocumentBuilder();
  78. InputSource inputSource = new InputSource(new StringReader(xml));
  79. Document xmlDoc = builder.parse(inputSource);
  80. Node root = xmlDoc.getDocumentElement();
  81. NodeList rootchildren = root.getChildNodes();
  82. Element newroot = xmlDoc.createElement(root.getNodeName());
  83. for (int i = 0; i < rootchildren.getLength(); i++) {
  84. newroot.appendChild(rootchildren.item(i).cloneNode(true));
  85. }
  86. xmlDoc.replaceChild(newroot, root);
  87. DOMSource requestXMLSource = new DOMSource( xmlDoc.getDocumentElement() );
  88. StringWriter requestXMLStringWriter = new StringWriter();
  89. StreamResult requestXMLStreamResult = new StreamResult( requestXMLStringWriter );
  90. transformer.transform( requestXMLSource, requestXMLStreamResult );
  91. String modifiedRequestXML = requestXMLStringWriter.toString();
  92.  
  93. return modifiedRequestXML;
  94. } catch (Exception e) {
  95. System.out.println("Could not parse message as xml: " + e.getMessage());
  96. }
  97. return "";
  98. }
  99.  
  100. before xml = <?xml version="1.0" encoding="UTF-8"?><Payment xmlns="http://api.com/schema/store/1.0"><Store>abc</Store></Payment>
  101. afterNsRemoval = <?xml version="1.0" encoding="UTF-8"?><Payment><Store>abc</Store></Payment>
  102.  
  103. public static String toString(Document doc) {
  104. try {
  105. StringWriter sw = new StringWriter();
  106. TransformerFactory tf = TransformerFactory.newInstance();
  107. ...
  108.  
  109. import java.io.*;
  110.  
  111. import javax.xml.parsers.*;
  112. import javax.xml.transform.*;
  113. import javax.xml.transform.dom.DOMSource;
  114. import javax.xml.transform.stream.StreamResult;
  115. import javax.xml.transform.stream.StreamSource;
  116.  
  117. import org.w3c.dom.Document;
  118. import org.xml.sax.InputSource;
  119. import org.xml.sax.SAXException;
  120.  
  121. public class XPathUtils {
  122. public static void main(String[] args) {
  123.  
  124. String xml = "<?xml version="1.0" encoding="UTF-8"?><Payment xmlns="http://api.com/schema/store/1.0"><Store>abc</Store></Payment>";
  125. String afterNsRemoval = removeNameSpace(xml);
  126. System.out.println("afterNsRemoval = " + afterNsRemoval);
  127. }
  128.  
  129. public static String removeNameSpace(String xml) {
  130. try{
  131. String xslStr = String.join("n",
  132. "<xsl:transform xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">",
  133. "<xsl:output version="1.0" encoding="UTF-8" indent="no"/>",
  134. "<xsl:strip-space elements="*"/>",
  135. " <xsl:template match="@*|node()">",
  136. " <xsl:element name="{local-name()}">",
  137. " <xsl:apply-templates select="@*|node()"/>",
  138. " </xsl:element>",
  139. " </xsl:template>",
  140. " <xsl:template match="text()">",
  141. " <xsl:copy/>",
  142. " </xsl:template>",
  143. "</xsl:transform>");
  144.  
  145. // Parse XML and Build Document
  146. DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
  147. InputSource is = new InputSource();
  148. is.setCharacterStream(new StringReader(xml));
  149. Document doc = db.parse (is);
  150.  
  151. // Parse XSLT and Configure Transformer
  152. Source xslt = new StreamSource(new StringReader(xslStr));
  153. Transformer tf = TransformerFactory.newInstance().newTransformer(xslt);
  154.  
  155. // Output Result to String
  156. DOMSource source = new DOMSource(doc);
  157. StringWriter outWriter = new StringWriter();
  158. StreamResult strresult = new StreamResult( outWriter );
  159. tf.transform(source, strresult);
  160. StringBuffer sb = outWriter.getBuffer();
  161. String finalstring = sb.toString();
  162.  
  163. return(finalstring);
  164.  
  165. } catch (Exception e) {
  166. System.out.println("Could not parse message as xml: " + e.getMessage());
  167. }
  168. return "";
  169. }
  170. }
Add Comment
Please, Sign In to add comment