I am trying to 'GET' a rss feed.
public RssFeed(String url) {
_url = url;
String res = this.api.get(url);
ByteArrayInputStream bis = new ByteArrayInputStream(res.getBytes());
try {
bis.close();
} catch (IOException e) {
e.printStackTrace();
}
XMLDecoder decoder = new XMLDecoder(bis);
try {
Object xml = decoder.readObject();
_response = xml.toString();
} catch(Exception e) {
e.printStackTrace();
} finally {
decoder.close();
}
}
When I check what's inside of 'res'. It appears to get this entire XML.
But then, I am trying to decode it and I get:
java.lang.IllegalArgumentException: Unsupported element: rss
Can someone help me with that? I am new to Java.
Thanks!
XMLDecoder is meant to be used on elements created by XMLEncoder. Since you're scraping this XML from the web, the elements in this XML may not be valid according to these classes. Use a more generic XML parser, such as DocumentBuilder::parse() to handle this.
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
try {
builder.parse(url);
} catch (IOException e) {
e.printStackTrace();
} catch (SAXParseException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
Related
In my app I need to read the content of an element from an xml file.
So I write in my LocalRead.java class the method "getValueOfElement" in this way:
[...]
public String getValueOfElement (String filename, String what){
try{
File xmlDocument = new File ("/Unknow_Path/"+filename);
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.parse(xmlDocument);
String lookingFor = document.getElementsByTagName(what).item(0).getTextContent();
return lookingFor;
} catch (FileNotFoundException e) {
System.err.println("----------------- File not found -----------------");
e.printStackTrace();
return "";
} catch (ParserConfigurationException e) {
System.err.println("----------------- Error creating DocumentBuilder -----------------");
e.printStackTrace();
return "";
} catch (SAXException e) {
System.err.println("----------------- Error creating the document(Sax) -----------------");
e.printStackTrace();
return "";
} catch (IOException e) {
System.err.println("----------------- Error creating the document(IO) -----------------");
e.printStackTrace();
return "";
}
}
[...]
As you can see, when I create the File "xmlDocument" I don't know the path where my xml file is. I used this class to create the file.
import android.content.Context;
import java.io.FileOutputStream;
import java.io.IOException;
public class FileBuilder {
private String xmlContent;
private String filename;
private Context context;
private FileOutputStream outputStream;
public FileBuilder(Context context){
this.context = context;
}
public boolean createUserFile(String username, String password){
this.xmlContent = "<?xml version='1.0' encoding='UTF-8'?>\n" +
"<giocatore>\n" +
"<username>"+username+"</username>\n" +
"<password>"+password+"</password>\n" +
"</giocatore>";
this.filename = "[D&D]User.xml";
try{
outputStream = context.openFileOutput(filename, context.MODE_PRIVATE);
outputStream.write(xmlContent.getBytes());
outputStream.close();
System.out.println("----------------- File created -----------------");
return true;
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
}
How can I find out what the path is?
Like CommonsWare says in the comments, Document can parse an InputStream. So you can load the file as a Document using openFileInput(). Here the complete code:
[...]
public String getValueOfElement (String filename, String what){
try{
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.parse(context.openFileInput(filename));
String lookingFor = document.getElementsByTagName(what).item(0).getTextContent();
return lookingFor;
} catch (FileNotFoundException e) {
System.err.println("----------------- File not found -----------------");
e.printStackTrace();
return "";
} catch (ParserConfigurationException e) {
System.err.println("----------------- Error creating DocumentBuilder -----------------");
e.printStackTrace();
return "";
} catch (SAXException e) {
System.err.println("----------------- Error creating the document(Sax) -----------------");
e.printStackTrace();
return "";
} catch (IOException e) {
System.err.println("----------------- Error creating the document(IO) -----------------");
e.printStackTrace();
return "";
}
}
[...]
Remember that openFileInput() need a Context.
Thanks to #CommonsWare for the answer
I need to pass some Bitmaps from one activity to another, and, since the size limit of the Bundle won't let me pass these images (even using a byte array*), I thought that I could use a getter method between these Activities.
-But, since I'm still not a master in Android (Java), I don't know if that would make any difference, and, if it does, what should I watch out for when using it.
the byte array did reduce the total size(at about 60%), but it still wasn't enough
scaling down is a way out, but just in case any other solution works
save your object in a file
private void saveDataToFile() {
FileOutputStream fileOutputStream = null;
try {
fileOutputStream = getContext().openFileOutput("fileName", Context.MODE_PRIVATE);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (NullPointerException e) {
}
ObjectOutputStream objectOutputStream = null;
try {
objectOutputStream = new ObjectOutputStream(fileOutputStream);
} catch (IOException e) {
e.printStackTrace();
} catch (NullPointerException e) {
}
try {
if (objectOutputStream != null) {
objectOutputStream.writeObject(yourObject); //which data u want to save
}
} catch (IOException e) {
e.printStackTrace();
}
try {
if (objectOutputStream != null) {
objectOutputStream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
Retrieve the object from another activity
private void getDataFromFile() {
FileInputStream fileInputStream = null;
try {
fileInputStream = getContext().openFileInput("fileName");
} catch (FileNotFoundException e) {
e.printStackTrace();
return;
}
ObjectInputStream objectInputStream = null;
try {
objectInputStream = new ObjectInputStream(fileInputStream);
} catch (IOException |NullPointerException e) {
e.printStackTrace();
}
try {
yourObject = (ObjectClass) objectInputStream.readObject();
} catch (IOException e) {
e.printStackTrace();
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
try {
objectInputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
Pass through Uri by writing getter method in POJO class
If you want to use getter setter, just create URI of your bitmap and pass to setter method in POJO class and then retrieve using getter method of POJO class.
I'm trying to parse SVG file to get paths using Xpath within android application. Native java parse the path in following way.
try {
Document document = builder.parse(
new FileInputStream("c:\\employees.xml"));
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
I try to do using FileDescriptor as follows.
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
try {
builder = factory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
AssetManager assetManager = getBaseContext().getAssets();
AssetFileDescriptor assetFileDescriptor = null;
try {
assetFileDescriptor = assetManager.openFd("android.svg");
} catch (IOException e) {
e.printStackTrace();
}
FileDescriptor fileDescriptor = assetFileDescriptor.getFileDescriptor();
FileInputStream stream = new FileInputStream(fileDescriptor);
try {
Document document = builder.parse(stream);
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
However my app stopped working. What's wrong in my code?
You do not need FileDescriptor. Try as follows.
InputStream input = assetManager.open("android.svg"); //From your asset folder
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = null;
try {
builder = factory.newDocumentBuilder();
} catch (ParserConfigurationException e) {
e.printStackTrace();
}
And parse your input to builder.
Document document = builder.parse(input);
When I run HP fortify the following code is given as a XML External Entity injection.Problem line is specified as Error Line.Any Help is appreciated.
private Document parseXmlString(String stringname, boolean validating) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setValidating(validating);
ByteArrayInputStream is = new ByteArrayInputStream(stringname.getBytes());
Document doc = factory.newDocumentBuilder().parse(is);//Error Line
return doc;
} catch (SAXException e) {
// A parsing error occurred; the xml input is not valid
} catch (ParserConfigurationException e) {
} catch (IOException e) {
}
return null;
}
I hope this is what you are looking for:
https://www.owasp.org/index.php/XML_External_Entity_(XXE)_Processing
I use SimpleXML to save a simple POJO into XML file and then read it back. I follow this tutorial. The file is successfully created, but the reading part is just simply not working. It throws the second exception which comes from serializer.read.
Room room = new Room("1");
Serializer serializer = new Persister();
try {
File ff = new File("room.xml");
serializer.write(room, ff);
} catch (FileNotFoundException e) {
System.out.println("FileNotFoundException\n");
} catch (Exception e) {
System.out.println("Exception from serializer.write\n");
}
try {
File ffi = new File("room.xml");
Room aroom = serializer.read(Room.class, ffi);
System.out.println("RoomName: " + aroom.getRid() + "\n");
} catch (FileNotFoundException e) {
System.out.println("FileNotFoundException\n");
} catch (Exception e) {
System.out.println("Exception from serializer.read\n");
}
Any hint?
Make sure you have a default constructor in Room.
public Room(){
}
Alternatively, make sure your constructor looks like this:
public Room(#Attribute(name="rid") String rid){
this.rid = rid;
}