Results 1 to 7 of 7

Thread: Convert XML string into DOM

  1. #1
    Join Date
    Mar 2010
    Posts
    52

    Convert XML string into DOM

    I am having question related to convert XML string to the DOM document. Actually I want to convert the XML string to DOM, and need to know how to do this in Java. The Document.Parse() function is not having the things that will take the Strings as the parameters. I am not having the idea related the best way for doing this. Any help related to this will be appreciated.

  2. #2
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Convert XML string into DOM

    Convert XML String to Document with StringReader.
    Code:
    String StringXml; 
    DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
           
    DocumentBuilder build = fact.newDocumentBuilder();
    Document doc = build.parse(new InputSource(new StringReader(StringXml)));
    You can use this snippet of code for converting the XML string to the document and you can do this with the help of string reader. If face any other problem related to this then let me know I will try to solve the problem.

  3. #3
    Join Date
    Mar 2010
    Posts
    52

    Re: Convert XML string into DOM

    Thank you for the help now I can easily convert XML String to Document with StringReader. But now I have one more issue.
    TestStr String = "<? Xml version = '1 .0 '?> <root> <El1> <title> Chapter1 </ title> </ EL1>
    <El2> <title> Chapter2 </ title> </ El2> </ root>
    And I want to convert this string into Document Dom to make subsequent treatments. (But without a file) wholesale before I did the treatment from a file with the following code:
    Code:
    public static Document ReadXmlFile (String XmlFiles)
    	 (		
    		 Document document = null;
    		
    		 try
    		 (			
    			 / / Create a factory records
    			 DocumentBuilderFactory factory = DocumentBuilderFactory. NewInstance ();								
    			 / / Create a document builder
    			 DocumentBuilder builder = factory. NewDocumentBuilder ();								
    			 / / Read the contents of an XML file with DOM
    			 File xml = new File (XmlFiles);
    			 document = builder. parse (xml);	
    			
    			
    		 )
    		 catch (ParserConfigurationException phr)
    		 (
    			 System. Out. System.out.println ("Error DOM parser configuration");
    			 System. Out. System.out.println ("when calling to fabrique.newDocumentBuilder ();");
    			 phr. printStackTrace ();
    			 System. Exit (0);
    		 )
    		 catch (SAXException se)
    		 (
    			 System. Out. System.out.println ("Error parsing document");
    			 System. Out. System.out.println ("when calling to construteur.parse (xml)");
    			 System. Out. System.out.println ("DTDs are they have become in the directory?");
    			 be. printStackTrace ();
    			 System. Exit (0);
    		 )
    		 catch (IOException ioe)
    		 (
    			 System. Out. System.out.println ("Error input / output");
    			 System. Out. System.out.println ("when calling to construteur.parse (xml)");
    			 ioe. printStackTrace ();			
    			 System. Exit (0);
    		 )
    		
    		 return document;
    	 )
    I will be waiting for your reply.

  4. #4
    Join Date
    Jan 2008
    Posts
    1,521

    Re: Convert XML string into DOM

    The solution might be to turn in my chain after inputString to do something like this:
    document = builder. parse (mon_InputString_qui_represente_ma_chaine);
    If this is an idea that has function, if that does not solve the issue then you can try this snippet of code.
    Code:
    public static Node ConvertStringToNode (String Str) (Node result = null; 
    Document document; 
    try (
    / / create a document 
    DocumentBuilderFactory factory = DocumentBuilderFactory factory. newInstance () 
    / / create a document builder 
    DocumentBuilder builder = factory . newDocumentBuilder ()
     / / read the contents of an XML file with DOM
     StringBuffer buffer = new StringBuffer (Str) = new ByteArrayInputStream ByteArrayInputStream bis1 (Buffer. toString (). getBytes ("UTF-8")) document = manufacturer. parse (bis1); 
    Element root = document. getDocumentElement ()
     result = root. getParentNode ()
    ;) catch (ParserConfigurationException phr) 
    (
    System. out. System.out.println ("\ n -> Utilities.java / ConvertStringToNode"); 
    System. out. System.out.println ("Configuration Error DOM parser");
     System. out. System.out.println ("when calling to fabrique.newDocumentBuilder ();
    ");
     phr. printStackTrace (); 
    System. exit (0) ;
    ) 
    catch (SAXException se) 
    (
    System. out. System.out.println ("\ n -> Utilities.java / ConvertStringToNode") 
    System. out. System.out.println ("Error parsing document"); 
    System. out. System.out.println (" when calling to construteur.parse (xml) ");
     System. out. System.out.println (" DTDs are they have become in the directory? ") is. printStackTrace (); 
    System. exit (0);
    ) 
    catch ( IOException ioe)
     (System. out. System.out.println ("\ n -> Utilities.java / ConvertStringToNode")
     System. out. System.out.println ("Error input / output");
     System. out. System.out.println ("at the construteur.parse call (xml) ");
     ioe. printStackTrace (); System. exit (0);
    ) return result;
    )

  5. #5
    Join Date
    Feb 2010
    Posts
    592

    Re: Convert XML string into DOM

    You just need to search the google for these things I will suggest you to search the method for converting the XML string in the DOM document. If you want to convert the string into a flow reading then you just need to insert this code. “ ByteArrayInputStream monFlux = new ByteArrayInputStream (maString. getBytes ("UTF-8")); “ I am sure this will resolve the issue.

  6. #6
    Join Date
    Feb 2010
    Posts
    570

    Re: Convert XML string into DOM

    I am a novice to this field and I want to know how to convert a Document (a DOM) into a String? This is to save in the db or send a jsp page. Then there jtidy who can bring out the result on a outputStream, but then I do not know how to convert it to String outputStream. In short I find on the net, I found some solutions but the thing is that they are not working. You should have the solution for this problem.

  7. #7
    Join Date
    Feb 2010
    Posts
    178

    Re: Convert XML string into DOM

    Just insert this snippet of code and execute it will convert the Document in to a string. But before you try to do this it will better to get the complete information about this conversion, I mean theoretical knowledge. Or else there are chances that you will face difficulty while converting the document.

    Code:
    ByteArrayOutputStream Outputfile = new ByteArrayOutputStream (); 
    tidy.pprint (doc Outputfile ); 
    String convertString = Outputfile .toString (); 
    System.out.println (convertString);

Similar Threads

  1. Convert string into int in C
    By screwball in forum Software Development
    Replies: 4
    Last Post: 22-12-2011, 08:47 PM
  2. How to convert string into int in Java
    By Zool in forum Software Development
    Replies: 3
    Last Post: 09-11-2009, 12:41 PM
  3. How to convert string to int
    By Zavier in forum Software Development
    Replies: 3
    Last Post: 04-06-2009, 06:24 PM
  4. Convert a string in decimal
    By FlayoFish in forum Software Development
    Replies: 3
    Last Post: 23-04-2009, 12:18 PM
  5. Convert a string to bytes
    By $tatic in forum Software Development
    Replies: 4
    Last Post: 18-02-2009, 02:29 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,126,858.12766 seconds with 17 queries