I want to read xml from url:
URL url = new URL("http://192.168.2.20/test.xml");
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(url.openStream()));
doc.getDocumentElement().normalize();
This works. But when I want to read from a url that needs authentication this method fails.
So for
url = new URL("http://admin:pass#192.168.2.20/test.xml");
it does not work.
Any ideas?
Something, like this, may be? http://blogs.deepal.org/2008/01/sending-basic-authentication-using-url.html
Related
Can we create/construct a custom xml packet in android? I need to create an xml packet in the format shown below and send it to the server.
Sending a packet to the server can be done using the DatagramSocket and DatagramPacket according to the links similar to this link. But how do I accomplish the task of creating a custom xml packet? Couldn't find much help in google.
xml format:
<a_list>
<packet_count>12</packet_count>
<info>
<id>12345</id>
</info>
</a_list>
try out this-
String Data="<xml></xml>"
Document docData= null;
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(Data));
docData = db.parse(is);
Element baseRoot=docData.getDocumentElement();
Element root = docData.createElement("a_list");
baseRoot.appendChild(root);
Element childpack = docData.createElement("packet_count");
root.appendChild(childpack);
Text text = docData.createTextNode("12");
childpack.appendChild(text);
Element childinfo = docData.createElement("info");
root.appendChild(childinfo );
Element childid = docData.createElement("id");
childinfo.appendChild(childid);
text = docData.createTextNode("12345");
childinfo.appendChild(text);
I need to parse a xml document in my Android application and I'm using Dom parser. Encoding in my xml file is set to UTF-8. The code I'm using for parsing is as follows:
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputStream inStream = getAssets().open("words.xml");
InputSource inSource = new InputSource(inStream);
inSource.setEncoding("UTF-8");
Document doc = db.parse(inSource);
But the problem is that I get an illegal character exception. The node which is problematic has the following structure:
<obriši>
<item>obriši</item>
<item>ukloni</item>
</obriši>
What could be the problem?
Try with
inSource.setEncoding("windows-1251");
There are a few posts re: this topic but can't figure out why this won't work.
Keep getting an IOException. Guessing it can't find the file. Cheers.
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse("//res/xml/xml_data.xml");
Change yours with this
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse("res/xml/xml_data.xml");
Your URL is incorrect. Resources take the form:
"android.resource://[package]/[res type]/[res name]"
or
"android.resource://[package]/[res id]
ie
"android.resource://com.org.example/xml/xml_data" // No extension
"android.resource://com.org.example/" + R.xml.xml_data
i am new in android development, i don't know how to parse data from xml, so please help.
this is my Xml which i have to parse.
<MediaFeedRoot>
<MediaTitle>hiiii</MediaTitle>
<MediaDescription>hellooooo.</MediaDescription>
<FeedPath>how r u</FeedPath>
</MediaFeedRoot>
Thanx in advance.
I dont understand that why people ask the question here without searching properly on net.Please do remember that search on net before asking anything here....
Below is the link where you can find a very good tutorial about xml parsing...
http://www.androidpeople.com/android-xml-parsing-tutorial-%E2%80%93-using-domparser
My suggestion is starting from the basic step:
think about your xml file connection: url? local?
instance a DocumentBuilderFactory and a builder
DocumentBuilder dBuilder =
DocumentBuilderFactory.newInstance().newDocumentBuilder();
OR
URLConnection conn = new URL(url).openConnection();
InputStream
inputXml = conn.getInputStream();
DocumentBuilder docBuilder = DocumentBuilderFactory.newInstance()
.newDocumentBuilder();
Document xmlDoc = docBuilder.parse(inputXml);
Parsing XML file:
Document xmlDom = dBuilder.parse(xmlFile);
After that, it turns a xml file into DOM or Tree structure, and you have to travel a node by node.
In your case, you need to get content. Here is an example:
String getContent(Document doc, String tagName){
String result = "";
NodeList nList = doc.getElementsByTagName(tagName);
if(nList.getLength()>0){
Element eElement = (Element)nList.item(0);
String ranking = eElement.getTextContent();
if(!"".equals(ranking)){
result = String.valueOf(ranking);
}
}
return result;
}
return of the getContent(xmlDom,"MediaTitle") is "hiiii".
Good luck!
I have created an xml file in the device's internal storage as described on the android developers website. I now want to parse the file using DOM parser. What do i need to do to make the DOM parser read my XML file??
Here's a snippet:
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document dom = db.parse(new InputSource(new StringReader(data)));
dom.getDocumentElement().normalize();
What do i need to put in the place of "data" in:
Document dom = db.parse(new InputSource(new StringReader(data)));
I know it's silly but any help would be appreciated.
You can make a input stream of the xml string like below and then getting nodes you can parse to get values.
InputStream is = new ByteArrayInputStream(theXMLString.getBytes("UTF-8"));
// Build XML document
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(is);
Remember you are passing xml file as a string.
You can give FileInputStream in inputsource
Document dom = db.parse(new InputSource(new FileInputStream(data)));
For reading XML file, you should try below
FileInputStream in = new FileInputStream("/sdcard/text.txt");
StringBuffer data = new StringBuffer();
InputStreamReader isr = new InputStreamReader(in);
BufferedReader inRd = new BufferedReader(isr);
String text;
while ((text = inRd.readLine()) != null) {
inLine.append(text);
inLine.append("\n");
}
in.close();
String finalData =data.toString(); // Here is your data.
Hope above may useful to you.
Try this code for parsing from Asset folder using DOM Parser :
DocumentBuilderFactory DBF;
DocumentBuilder DB;
Document dom;
Element elt;
DBF = DocumentBuilderFactory.newInstance();
DB = DBF.newDocumentBuilder();
dom = DB.parse(new InputSource(getAssets().open("city.xml")));
elt = dom.getDocumentElement();
NodeList items = elt.getElementsByTagName("item");
where item is Node element, add try ctch block as per the requirements.