XML processing and creating a resource in parallel in Java - java

I am writing a app, which has to create multiple Resources. The input is a XML. I need to parse the XML, create the Resources in parallel and update the responses in the Ouputs.
<Components>
<Resources>
<CreateResourceARequest> ...</CreateResourceARequest>
<CreateResourceBRequest>...</CreateResourceBRequest>
<CreateResourceCRequest>...</CreateResourceCRequest>
</Resources>
<Outputs>
<CreateResourceAResponse>...</CreateResourceAResponse>
<CreateResourceBResponse>...</CreateResourceBResponse>
<CreateResourceCResponse>...</CreateResourceCResponse>
</Outputs>
<Components>
Each of the ResourceRequests are handled by a specific Classes.
What is the best way to create Resources in parallel, aggregate the results and update the xml ?

you will parse xml file in single thread anyway because the file is linear. but you can collect you parsers for each CreateResourceCRequest in a set and start them all in parallel threads after parsing file

Related

Index single Xml-file with Lucene

I'm writing a Java application and want to index an Xml-file with Lucene so I can search for a drug that has a given target. The file size is 400MB and it is filled with over 8000 drug-entries.
<drug type="biotech" created="2005-06-13" updated="2015-11-27">
<drugbank-id primary="true">DB00001</drugbank-id>
<drugbank-id>BIOD00024</drugbank-id>
<drugbank-id>BTD00024</drugbank-id>
<name>Lepirudin</name>
....
<targets>
<target position="1">
<id>BE0000767</id>
<name>Epidermal growth factor receptor</name>
....
</target>
....
</targets>
</drug>
<drug>
....
</drug>
How can I index this file so one drug-entry is one Document?
If someone has some useful links/resources or tips on how to index this Xml please let me know :)
The most flexible strategy is usually to just use SolrJ through a small java application that reads the file and transforms it to a suitable format for indexing in Solr. That way you can easily preprocess certain fields before they're received by Solr.
Another option is to use XSL to transform the XML file into something that Solr understands. This can be used either server-side (as with XSLTUpdateRequestHandler linked) or client-side (transform an XML document into an update request and submit it to the standard request handler).

Transform xml with data from different sources to another xml using java

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.

Merging template XML with Old Data XMLs

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.

Dynamic XML creation in Java

I am trying to dynamically y create an XML file in Java to display a timetable. I have created a DTD for my XML file and I have an XSL file I would like to use to transform the XML. I don't know exactly how to continue.
What I've tried so far is onClick of some button a Servlet is called which generates the string of the content of the XML file (inserting the dynamic parts of the XML into the String. I now have a String containing the content of the XML file. I would now like to transform the XML file using an XSL file i have on my server and display the result in the page which has called the Servlet (doing this via AJAX).
I'm not sure if I'm in the direction, perhaps I shouldn't even create the XML code in String form from the beginning. So my question is, how do I continue from here? how do I transform the XML string, using the XSL file, and send it as a response to the AJAX call so I can plant the generated code into the page? Or if this is not the way to do it, how do I create a dynamic XML file in a different way producing the same result?
You can use JAXP for this. It's part of standard Java SE API.
StringReader xmlInput = new StringReader(xmlStringWhichYouHaveCreated);
InputStream xslInput = getServletContext().getResourceAsStream("file.xsl"); // Or wherever it is. As long as you've it as an InputStream, it's fine.
Source xmlSource = new StreamSource(xmlInput);
Source xslSource = new StreamSource(xslInput);
Result xmlResult = new StreamResult(response.getOutputStream()); // XML result will be written to HTTP response.
Transformer transformer = TransformerFactory.newInstance().newTransformer(xslSource);
transformer.transform(xmlSource, xmlResult);
Depending on how complicated and large your XML is going to be I would suggest two options. For small, simple structures Java's DOM implementation (Document) will suffice.
If your XML is more elaborate I would look into JAXB. The benefit there is that there are tools that automatically create Java classes from an XML schema (XSD). So you'd have to transform your DTD into an XSD first, but that shouldn't be a problem. You end up with plain data transfer objects (plain objects with getters/setters for the values of the corresponding XML elements) and parsing/encoding plus setting namespaces correctly is done for you. It's quite convenient but can also be a bit of an overkill for simple XML structures.
In both cases, you will end up with a Document instance that you can finally transform using JAXP.
Apache XMLBeans are a nice solution to serializing to and from XML. Here's what you need to do:
Download XMLBeans from http://www.apache.org/dyn/closer.cgi/xmlbeans/binaries
Use the XMLBeans inst2xsd executable (in the bin dir0 to convert your DTD to an XSD
Use the XMLBeans ANT task to convert the XSD into classes which you can use in your app
Here's an example ANT script to use XMLBeans to create the classes:
<project name="my_project" basedir="..">
<property name="my_project.project.path" value="${basedir}"/>
<property name="xbean.dir" value="C:/lib/xmlbeans-2.2.0/lib" />
<path id="classpath">
<fileset dir="${xbean.dir}" includes="**/*.jar" />
</path>
<taskdef name="xmlbean" classname="org.apache.xmlbeans.impl.tool.XMLBean" classpathref="classpath" />
<xmlbean schema="${testing_project.project.path}/my.xsd" srcgendir="${my_project.project.path}/src-tms-template-filter-fields" classgendir="${my_project.project.path}/bin">
<classpath><path refid="classpath" /></classpath>
</xmlbean>
You'll now have nice Java classes which you can use for clean code to create the XML from the data stored in your DB. Use BalusC's answer for the XSLT.

how to parse 2 xml files in android

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

Categories