- Decode XML message in Java
- Decode XML message in Java
- How to disable Decoding encoded XML Special characters in Java using DOM parser in Java [duplicate]
- Decode large base64 from xml in java: OutOfMemory
- Decoding xml in java
- Constructor Summary
- Method Summary
- Methods inherited from class java.lang.Object
- Constructor Detail
- XMLDecoder
- XMLDecoder
- XMLDecoder
- XMLDecoder
- XMLDecoder
- Method Detail
- close
- setExceptionListener
- getExceptionListener
- readObject
- Class XMLDecoder
- Constructor Summary
- Method Summary
- Methods declared in class java.lang.Object
- Constructor Details
- XMLDecoder
- XMLDecoder
- XMLDecoder
- XMLDecoder
- XMLDecoder
- Method Details
- close
- setExceptionListener
- getExceptionListener
- readObject
- setOwner
- getOwner
- createHandler
- Decoding xml in java
- Constructor Summary
- Method Summary
- Methods declared in class java.lang.Object
- Constructor Detail
- XMLDecoder
- XMLDecoder
- XMLDecoder
- XMLDecoder
- XMLDecoder
- Method Detail
- close
- setExceptionListener
- getExceptionListener
- readObject
- setOwner
- getOwner
- createHandler
Decode XML message in Java
Question: I am storing XML file in DB as string format, before storing in DB i am making sure that the document is well formed and converting all special characters , , , , to etc., when i get the string data back form DB and converting the String to XML document using DOM Parser, but when i convert these encoded special characters ( etc.) are being converted back to actual characters ( , ). Solution 4: If your file can get that big, never use a DOM parser.
Decode XML message in Java
i get the following String through a TCP connection:
i need to extract from this string only the field inside the message.
DOMParser parser = new DOMParser(); try < parser.parse(new InputSource(new java.io.StringReader(fromAAI))); Document doc = parser.getDocument(); String message = doc.getDocumentElement().getTextContent(); System.out.println("___" + message); >catch (SAXException e) < >catch (IOException e)
but of course it just print the TIME. can anyone help me with some example?
I suggest you to use SAXParser than DOM.. It is event based and It will be very easy for you to get data between the tags.
You can evaluate a XPath expression for this purpose:
DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); Document document = builder.parse(new InputSource(new java.io.StringReader(fromAAI))); XPath path = XPathFactory.newInstance().newXPath(); String expression = "//TIME"; String time = (String) path.evaluate(expression, document, XPathConstants.STRING);
But a solution using StAX would be more efficient.
this is what i did and it work perfectly;
public class ReadXml < public ReadXml(String xmlString) < try < SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser saxParser = factory.newSAXParser(); DefaultHandler handler = new DefaultHandler() < boolean bfpld = false; boolean bfpla = false; boolean bfplc = false; boolean btime = false; public void startElement(String uri, String localName,String qName, Attributes attributes) throws SAXException < if (qName.equals("FPLDDATA")) < for (int i = 0 ; i < attributes.getLength() ; i++) < new ShowMessage(attributes.getQName(i) + " VALUE:" + attributes.getValue(i)); bfpld = true; >if (qName.equals("FPLADATA")) < for (int i = 0 ; i < attributes.getLength() ; i++) < new ShowMessage(attributes.getQName(i) + " VALUE:" + attributes.getValue(i)); bfpla = true; >if (qName.equals("TIME")) < btime = true; >if (qName.equals("FPLC")) < for (int i = 0 ; i < attributes.getLength() ; i++) < new ShowMessage(attributes.getQName(i) + " VALUE:" + attributes.getValue(i)); >bfplc = true; > > public void characters(char ch[], int start, int length) throws SAXException < if (bfpld) < new ShowMessage("FPLDDATA : " + new String(ch, start, length)); bfpld = false; >if (bfpla) < new ShowMessage("FPLADATA : " + new String(ch, start, length)); bfpla = false; >if (btime) < new ShowMessage("TIME : " + new String(ch, start, length)); btime = false; >if (bfplc) < new ShowMessage("FPLC : " + new String(ch, start, length)); bfplc = false; >> >; InputStream inputStream = new ByteArrayInputStream(xmlString.getBytes(StandardCharsets.UTF_8)); Reader reader = new InputStreamReader(inputStream,"UTF-8"); InputSource is = new InputSource(reader); is.setEncoding("UTF-8"); saxParser.parse(is, handler); > catch (Exception e) < e.printStackTrace(); >> >
thanks a lot for your helps.
Removing invalid XML characters from a string in java, All these answers so far only replace the characters themselves. But sometimes an XML document will have invalid XML entity sequences resulting in errors. For example, if you have in your xml, a java xml parser will throw Illegal character entity: expansion character (code 0x2 at .
How to disable Decoding encoded XML Special characters in Java using DOM parser in Java [duplicate]
I am storing xml file in DB as string format, before storing in DB i am making sure that the document is well formed and converting all special characters < , >, & , » , ‘ to < > etc.,
when i get the string data back form DB and converting the String to XML document using DOM Parser, but when i convert these encoded special characters ( < > etc.) are being converted back to actual characters ( < , >).
But I don’t want this automatic decoding that making by DOM parser, i just want the same encoded characters as it is.
How do i avoid this decoding happening form DOM parser
Have you tried escaping the entities, e.g. < , in the XML returned from the database before parsing it? For example:
Document document = documentBuilder.parse(new InputSource(new StringReader(xml.replaceAll("&", "&"))));
Java replace all * in string Code Example, Get code examples like «java replace all * in string» instantly right from your google search results with the Grepper Chrome Extension.
Decode large base64 from xml in java: OutOfMemory
I need to write a base64 encoded element of an xml file into a separate file. Problem: the file could easily reach the size of 100 MB. Every solution I tried ended with the «java.lang.OutOfMemoryError: Java heap space». The problem is not reading the xml in general or the decoding process, but the size of the base64 block.
I used jdom, dom4j and xmlstreamreader to access the xml file. However, as soon as I want to access the base64 content of the respective element I get the mentioned error. I also tried an xslt using saxon’s base64binary -to-octets function, but of course with the same result.
Is there a way to stream this base64 encoded part into a file without getting the whole chunk in one single piece?
Apache Commons Codec has a Base64OutputStream , which should allow you to feed the XML data in a scalable way, by chaining the Base64OutputStream with a FileOutputStream .
You’ll need a representation of the XML as a String, so you may not even have to read it into a DOM structure at all.
PrintWriter printWriter = new PrintWriter( new Base64OutputStream( new BufferedOutputStream( new FileOutputStream("/path/to/my/file") ) ) ); printWriter.write(myXml); printWriter.close();
If the input XML file is too big, then you should read chunks of it into a buffer in a loop, writing the buffer contents to the output (i.e. a standard reader-to-writer copy).
I don’t think any XML api would let you access an element’s text as a stream rather than a String. If the String is 100 MB, then your only option is probably to change the JVM’s heap size until you don’t have any OutOfMemoryError :
java -Xmx256m your.class.Name
Try the StAX API (tutorial). For large text elements, you should get several text events which you need to push into a streaming Base64 implementation (like the one skaffman mentioned).
If your file can get that big, never use a DOM parser. Use a simple SAX approach to access the data elements, and stream the base64 data into Base64OutputStream as mentioned above.
Decoding xml in java
The XMLDecoder class is used to read XML documents created using the XMLEncoder and is used just like the ObjectInputStream . For example, one can use the following fragment to read the first object defined in an XML document written by the XMLEncoder class:
XMLDecoder d = new XMLDecoder( new BufferedInputStream( new FileInputStream("Test.xml"))); Object result = d.readObject(); d.close();
Constructor Summary
Method Summary
Creates a new handler for SAX parser that can be used to parse embedded XML archives created by the XMLEncoder class.
Methods inherited from class java.lang.Object
Constructor Detail
XMLDecoder
XMLDecoder
XMLDecoder
public XMLDecoder(InputStream in, Object owner, ExceptionListener exceptionListener)
XMLDecoder
public XMLDecoder(InputStream in, Object owner, ExceptionListener exceptionListener, ClassLoader cl)
XMLDecoder
Creates a new decoder to parse XML archives created by the XMLEncoder class. If the input source is is null , no exception is thrown and no parsing is performed. This behavior is similar to behavior of other constructors that use InputStream as a parameter.
Method Detail
close
setExceptionListener
Sets the exception handler for this stream to exceptionListener . The exception handler is notified when this stream catches recoverable exceptions.
getExceptionListener
readObject
Class XMLDecoder
The XMLDecoder class is used to read XML documents created using the XMLEncoder and is used just like the ObjectInputStream . For example, one can use the following fragment to read the first object defined in an XML document written by the XMLEncoder class:
XMLDecoder d = new XMLDecoder( new BufferedInputStream( new FileInputStream("Test.xml"))); Object result = d.readObject(); d.close();
For more information you might also want to check out Long Term Persistence of JavaBeans Components: XML Schema, an article in The Swing Connection.
Constructor Summary
Method Summary
Creates a new handler for SAX parser that can be used to parse embedded XML archives created by the XMLEncoder class.
Methods declared in class java.lang.Object
Constructor Details
XMLDecoder
XMLDecoder
XMLDecoder
XMLDecoder
public XMLDecoder (InputStream in, Object owner, ExceptionListener exceptionListener, ClassLoader cl)
XMLDecoder
Creates a new decoder to parse XML archives created by the XMLEncoder class. If the input source is is null , no exception is thrown and no parsing is performed. This behavior is similar to behavior of other constructors that use InputStream as a parameter.
Method Details
close
setExceptionListener
Sets the exception handler for this stream to exceptionListener . The exception handler is notified when this stream catches recoverable exceptions.
getExceptionListener
readObject
setOwner
getOwner
createHandler
Creates a new handler for SAX parser that can be used to parse embedded XML archives created by the XMLEncoder class. The owner should be used if parsed XML document contains the method call within context of the
Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples. Other versions.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2023, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.
Decoding xml in java
The XMLDecoder class is used to read XML documents created using the XMLEncoder and is used just like the ObjectInputStream . For example, one can use the following fragment to read the first object defined in an XML document written by the XMLEncoder class:
XMLDecoder d = new XMLDecoder( new BufferedInputStream( new FileInputStream("Test.xml"))); Object result = d.readObject(); d.close();
For more information you might also want to check out Long Term Persistence of JavaBeans Components: XML Schema, an article in The Swing Connection.
Constructor Summary
Method Summary
Creates a new handler for SAX parser that can be used to parse embedded XML archives created by the XMLEncoder class.
Methods declared in class java.lang.Object
Constructor Detail
XMLDecoder
XMLDecoder
XMLDecoder
public XMLDecoder(InputStream in, Object owner, ExceptionListener exceptionListener)
XMLDecoder
public XMLDecoder(InputStream in, Object owner, ExceptionListener exceptionListener, ClassLoader cl)
XMLDecoder
Creates a new decoder to parse XML archives created by the XMLEncoder class. If the input source is is null , no exception is thrown and no parsing is performed. This behavior is similar to behavior of other constructors that use InputStream as a parameter.
Method Detail
close
setExceptionListener
Sets the exception handler for this stream to exceptionListener . The exception handler is notified when this stream catches recoverable exceptions.
getExceptionListener
readObject
setOwner
getOwner
createHandler
public static DefaultHandler createHandler(Object owner, ExceptionListener el, ClassLoader cl)
Creates a new handler for SAX parser that can be used to parse embedded XML archives created by the XMLEncoder class. The owner should be used if parsed XML document contains the method call within context of the
Report a bug or suggest an enhancement
For further API reference and developer documentation see the Java SE Documentation, which contains more detailed, developer-targeted descriptions with conceptual overviews, definitions of terms, workarounds, and working code examples.
Java is a trademark or registered trademark of Oracle and/or its affiliates in the US and other countries.
Copyright © 1993, 2023, Oracle and/or its affiliates, 500 Oracle Parkway, Redwood Shores, CA 94065 USA.
All rights reserved. Use is subject to license terms and the documentation redistribution policy.