hi i am a new developer and i want know how to do parsing of two xml files in a project.
I have 2 xml files. the first one is as follows
<?xml version="1.0"?>
<X>
<Y>
<Z>
A
</Z>
<packs>
<pack>
<packname>B</packname>
</pack>
</packs>
</Y>
</X>
The next xml files looks as follows
<s>
<t>
<question>abc</question>
<question>def</question>
<question>ghi</question>
</t>
</s>
The first XML file works for me. When i touch A it moves over to B. Now when i touch B i want to show the first question alone ie abc, Can it be done, it is not working for me
pls tell me how to move from one xml file to the other xml file
can anyone explain this with a sample codes....
Where should i store the 2 files to be parsed... I have tried storing it in raw folder in Resources.
Here is an overview of some XML parsers available for Android including some examples.
It depends on your application needs where you want to store your xml file (xml from a web service call mey remain temporarily in memory). Files like xml should be generally stores in the Raw folder.
You can use SAX Parser or Pull Parser to parse the xml. Following are some links for help:
SAXParser
Example
Related
i am new to xml parsing not able to decide how to parse this complex xml file in java .
I am able to parse simple xml file but when when it comes to complex xml file i am confused .Not able to read elements of xml using java .
Here is my sample xml file .
<?xml version="1.0"?>
<env:ContentEnvelope xsi:schemaLocation="http://fundamental.schemas.financial.jso.com/Fundamental/2011-07-07/
https://theshare.jso.com/sites/TRM-IA/Content%20Marketplace/Strategic%20Data%20Interfaces/SDI%20Schemas/Schemas/Fundamentals/2015-09-25/FundamentalMaster.xsd"
xmlns:esg="http://fundamental.schemas.financial.jso.com/ESGSupportingInfo/2011-07-07/"
xmlns:md="http://data.schemas.financial.jso.com/metadata/2010-10-10/"
xmlns:cr="http://fundamental.schemas.financial.jso.com/CoraxData/2012-10-25/"
xmlns:ful="http://fundamental.schemas.financial.jso.com/FundamentalLineItem/2011-07-07/"
xmlns:fun="http://fundamental.schemas.financial.jso.com/Fundamental/2011-07-07/"
xmlns:ir="http://fundamental.schemas.financial.jso.com/FinancialInstrumentRelationship/2011-07-07/"
xmlns:fl="http://fundamental.schemas.financial.jso.com/FinancialLineItem/2011-07-07/"
xmlns:pe="http://fundamental.schemas.financial.jso.com/FinancialPeriod/2011-07-07/"
xmlns:seg="http://fundamental.schemas.financial.jso.com/FinancialSegment/2011-07-07/"
xmlns:sr="http://fundamental.schemas.financial.jso.com/FinancialSource/2011-07-07/"
xmlns:sli="http://fundamental.schemas.financial.jso.com/StandardizedLineItem/2011-07-07/"
xmlns:ss="http://fundamental.schemas.financial.jso.com/StandardizedStatement/2011-07-07/"
xmlns:fs="http://fundamental.schemas.financial.jso.com/FinancialStatement/2011-07-07/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:env="http://data.schemas.tfn.jso.com/Envelope/2008-05-01/" minVers="1.0" majVers="3" pubStyle="Message">
<env:Header>
<env:Info>
<env:Id>urn:uuid:069527ab-2c10-48bb-b3d2-206f4e66e5d2</env:Id>
<env:TimeStamp>2016-12-23T10:09:09+00:00</env:TimeStamp>
</env:Info>
<fun:OrgId>20240</fun:OrgId>
<fun:PartitionId>1</fun:PartitionId>
</env:Header>
<env:Body minVers="0.0" majVers="1" contentSet="Fundamental">
<env:ContentItem action="Insert">
<env:Data xsi:type="fun:FundamentalDataItem">
<fun:Fundamental effectiveTo="9999-12-31T00:00:00+00:00" effectiveFrom="2013-06-29T00:55:15.313+00:00" uniqueFuamentalSet="0054341342">
<fun:OrganizationId objectType="Organization" objectTypeId="404510">42565596</fun:OrganizationId>
<fun:PrimaryReportingEntityCode>A4C67</fun:PrimaryReportingEntityCode>
<fun:TotalPrimaryReportingShares>567923000.00000</fun:TotalPrimaryReportingShares>
<fun:LocalLanguageId>505074</fun:LocalLanguageId>
<fun:IndustryGroups>
<fun:IndustryGroup validTo="9999-12-31T00:00:00+00:00" validFrom="1900-01-01T00:00:00+00:00">
<fun:GroupCode>BNK</fun:GroupCode>
<fun:GroupName languageId="505074">Bank</fun:GroupName>
<fun:TaxonomyId>1</fun:TaxonomyId>
<fun:IndustryGroupCodeId>3011649</fun:IndustryGroupCodeId>
</fun:IndustryGroup>
</fun:IndustryGroups>
<fun:GaapCode>CAG</fun:GaapCode>
<fun:ConsolidationBasis>Consolidated</fun:ConsolidationBasis>
<fun:IsFiling>true</fun:IsFiling>
<fun:ConsolidationBasisId>3013598</fun:ConsolidationBasisId>
<fun:GaapCodeId>3011536</fun:GaapCodeId>
<fun:Taxonomies>
<fun:Taxonomy>1</fun:Taxonomy>
</fun:Taxonomies>
<fun:WorldScopeIds>
<fun:WorldScopeId validTo="9999-12-31T00:00:00+00:00" validFrom="2012-03-31T00:00:00+00:00">C12436390</fun:WorldScopeId>
</fun:WorldScopeIds>
</fun:Fundamental>
</env:Data>
</env:ContentItem>
Definitely JAXB will help you here.
Since you are dealing with complex xml files, i would suggest below approach( i agree it's lengthy and manual but shall work fine).
1) Generate xsd schema out of given xml content
2) Create a JAXB project in eclipse and create and empty XSD file and write it with xsd schema generated above
3) To convert .xsd file to pojo Right click on .xsd file and generate JAXB classes
4) Now write a code to un-marshal the data and run it, this should give you a corresponding java class.
I have an xml file in which some elements contain certain values.For example:
<item>
<origin>
<![CDATA[KWI]]>
</origin>
<destination>
<![CDATA[DOH]]>
</destination>
</item>
I have an excel sheet containing the country codes and port code mapping as:
COUNTRY_CODE PORT_CODE MANAGING_PORT_STATION
KW KWI MPS1
QA DOH MPS2
In the output xml, i need to put it something like:
<itemOut>
<country><![CDATA[KW]]></country>
<managingPortStation>MPS1</managingPortStation>
<dest><![CDATA[DOH]]></dest>
</itemOut>
So in short, I need to combine some non xml sources into the output xml file based on the input xml file, along with the xml file.
To accomplish the above, what should I use? Is it possible via xslt? Or what API's are available with java. I have just skimmed through the jaxp. But is it worth spending more time for my case? I would prefer to do it with java,rather than xslt since I am more familiar with it.
I have about 3200 URLs to small XML files which have some data in the form of strings(obviously).The XML files are displayed(not downloaded) when I go to the URLs. So I need to extract some data from all those XMLs and save it in a single .txt file or XML file or whatever. How can I automate this process?
*Note: This is what the files look like. I need to copy the 'location' and 'title' from all of them and put them in one single file. Using what methodology can this be achieved?
<?xml version="1.0"?>
-<playlist xmlns="http://xspf.org/ns/0/" version="1">
-<tracklist>
<location>http://radiotool.com/fransn.mp3</location>
<title>France, Paris radio 104.5</title>
</tracklist>
</playlist>
*edit: Fixed XML.
It's easy enough with XQuery or XSLT, though the details will depend on how the URLs are held. If they're in a Java List, then (with Saxon at least) you can supply this list as a parameter to the following query:
declare variable urls as xs:string* external;
<data>{
for $u in $urls return doc($u)//*:tracklist
}</data>
The Java code would be something like:
Processor proc = new Processor();
XQueryCompiler c = proc.newXQueryCompiler();
XQueryEvaluator q = c.compile($query).load();
List<XdmItem> urls = new ArrayList();
for (url : inputUrls) {
urls.append(new XdmAtomicValue(url);
}
q.setExternalVariable(new QName("urls"), new XdmValue(urls));
q.setDestination(...)
run();
Have a look at the JSoup library here: http://jsoup.org/
It has facilities for pulling and fixing the contents of a URL, it is intended for HTML though, so I'm not sure it will be good for XML, but it is worth a look.
First let me say I am very new to Java. I've been trying to figure out how to append a chunk of XML to an existing XML file using Xstream.
Example XML:
<root>
<first>
<a>Some Value</a>
<b>Some Value B</b>
</first
<second>
<a>Another Value</a>
<b>Another Value B</b>
</second>
</root>
How would I go about appending the following using Xstream?
<third>
<a>More A</a>
<b>More B</b>
</third>
Have you followed the two minute tutorial of Xstream yet? This can be found here.
You should address some implementation choices first: which way to go with Xstream?
For example: is the XML document a large document or a small document (if small, you could use a DomDriver.If large, use a StaxDriver)?
Does your XML document uses namespaces? If so, be aware that not all Xstream parsers implement namespace awareness, see the Xstream faq.
More information can be found here to get you started.
Please provide us your SSCCE so that users can try out your code example.
See details on how to write a SSCCE here and here
Also include a small valid XML file in your SSCCE.
I have to a template XML and a old data XML which may not be having the same structure as the template.
I need to create a new data XML based on the template XML and populate it with values from the old data XML if they are present.
e.g.
Template XML -
<tag1></tag1>
<tag3></tag3>
Old Data XML -
<tag1>value1</tag1>
<tag2>value2</tag2>
New Data XML -
<tag1>value1</tag1>
<tag3></tag3>
I need to achieve this using Java.
Can this be done using Java XSL Transformers? Or do I need some External APIs?
There is an interesting thread here that talks about the same thing.
XSLT: A simple way to merge xml files
Hope this will help you.