i have a XML file in : /res/xml/countries.xml
and also i have a view with editText inside.
i want to search that text (users should type in editText) in my countries.xml file!
here is my xml file :
<?xml version="1.0" encoding="utf-8"?>
<countries>
<country>
<name>United State</name>
</country>
<countries>
If you want to read a whole document, use XML Parser, like Pull already mentioned. If you only want to pick out only one or few certain things, I would suggest XPath. Here is a good Tutorial on how to use XPath in Java.
First you need to parse that xml file containing the countries name. Store list of countries in any arraylist and then write any search algo to search the text found from that edittext.
Try this xpath tutorial...
You will have good learning experience...
This tutorial explain clearly how to parse an XML file : XML parser
Other tutorial simpler with XPath : Xparse tutorial
Related
I have some XML data inside an XML file which i want to pass to another application. I have used XMLPullPaser.
<?xml version="1.0" encoding="utf-8" ?>
<node1>
<node2>dd03</node2>
<node3>,17,0,,**<xml><cell>555</cell></xml>**,</node3>
</bintextobj>
node 3 contains the data as highlighted. The xml I want to pass as data of xml file. Is there any way of achieving this?
Use CDATA to store the value. This will cause the parser to treat the value of node3 like plain text.
<node3><![CDATA[,17,0,,**<xml><cell>555</cell></xml>**,]]></node3>
write xsl to extract the <xml>..</xml> and write into the new xml file which can be passed to the other applications.
I want to parse XML file using SaxParser. I'm trying to fetch the data associated to a tag or its attributes. The XML is in the following format.
<con>
<fig>
<abc>
<name xyz="">
<id>2</id>
</name>
</abc>
</fig>
</con>
I tried with couple of example but not succeeded in the fetching the data, I am requesting you to provide me any suggestion or and working example to increase my knowledge on parsing using SAX.
Use a DOM parser as your xml is very small and its easy to get what you are asking for.
There are my example and help available online, Please Google your question before posting on the stack over flow from next time.
Please check the following links which has good example for saxParsing
How to read XML file in Java – (SAX Parser)
Getting Attributes And its Value
hope this help you.
I'm trying to parse an XML file and insert some attributes in my database. I'm developing in JAVA and using SAX to parse the XML file.
My problem is that when I read an attribute in CDATA format I only get what the CDATA contains. Perhaps I wan't to keep the CDATA format?
For example with the XML below :
<?xml version="1.0" encoding="UTF-8"?>
<Bank>
<Account type="saving">
<Id>1001</Id>
<Name><![CDATA[<Jack> <Robinson>]]></Name>
<Amt>10000</Amt>
</Account>
<Account type="current">
<Id>1002</Id>
<Name>Sony Corporation</Name>
<Amt>1000000</Amt>
</Account>
</Bank>
I would like to get the Name and have it like this <![CDATA[<Jack> <Robinson>]]> and not only <Jack> <Robinson> which is what I am getting.
Can anyone help me with this issue please.
PS : Sorry for my English, I'm french.
Best regards,
Like #Quentin asked, I am curious why do you care about markup.
Did you consider appending <![CDATA[ and ]] using StringBuffer manually in your output.
I have an XML, for example
<root>
<config x="xxx" y="yyyy" z="zzz" />
<properties>blah blah blah </properties>
<example>
<name>...</name>
<decr>...</descr>
</example>
<example>
<name>...</name>
<decr>...</descr>
</example>
</root>
and I need to get nodes config, and properties and all values in it.
Thank you
Xpath can fetch you the data in the config tag. You need to create an expression first like this
expression="//root/config/#x", to get value of x,y,z.
For properties, follow this thread :
Parsing XML with XPath in Java
Hope this helps
DOM,DOM4J,SAX..
if the size of XML file is small,you can use DOM or DOM4J,but the size is big , you use the SAX
If you directly want to query or fetch data XPath can help, but if you want the data as Java Objects so that you can use it further then use JAXB
You can use SAX parser to read the xml manipulate its event based parsing and consumes more memory.
If your xml is big and requires lot of manipulations then go-for DOM/DOM4j either is good. DOM4L is very latest. DOM is widely used in industry.
Based on your requirement go for good parser.
Thanks,
Pavan
I have the following data in my XML file.
<main>
<Team Name="Development" ID="10">
<Emp Source="Business" Total="130" Active="123" New="12" />
<Emp Source="Business" Total="131" Active="124" New="13" />
</Team>
<Team Name="Testing" ID="10">
<Emp Source="Business" Total="133" Active="125" New="14" />
</Team>
</main>
I want to read above data & store values into arrays,Can any one help on these?
Not sure why you need to convert those xml into Arrays, anyhow you can read xml and parse it by several ways. Normally we use DOM or Stax Parser and a Tutorial link is here, also here is a Java SAX Parsing Example tutorial.
Hope this can help you to achieve your goal. Update your question again if you stuck anywhere.
You can use parser in JAVA to parse the XML document. The package in java for this purpose is javax.xml.parsers . DocumentBuilder parses XML into a Document and Document is a tree structured data structure that is DOM(Document Object Model) readable file. Its nodes can be traversed/ changed/ accessed by DOM methods.
Here is a very good tutorial on XML DOM: http://www.roseindia.net/xml/dom/
and more specifically: http://www.roseindia.net/xml/dom/accessing-xml-file-java.shtml
also you can always refer to w3school for more theory on DOM!