Solved

How can I split an xml file into two based on contents?

Posted on 2008-10-28
1
1,111 Views
Last Modified: 2013-11-23
I have one XML file  and I want to create two files out of it based on contents. Is there a way I can use Java and xslt to split one xml into two. Please look at the attached sample xml. I want to create two xml files out of it. One with type=city and the other with type=village
<main>
<item name="address" type="city">
<street>
  422 N California Dr
</street>
<city>
  San Francisco
</city>
</item>
<item name="address" type="village">
<street>
  422 Walrus Way
</street>
<town>
  Modesto
</town>
</item>
</main>

Open in new window

0
Comment
Question by:ddas_auth
1 Comment
 
LVL 8

Accepted Solution

by:
n_sachin1 earned 500 total points
ID: 22828940
There are multiple ways of doing it (depending upon your choice of parsers), but the base workflow would be:
1. Parse the DOM tree using java. Write two DOM documents based on the type.
2. Use XPath to retrieve nodes of type 'City', flush it as a document. repeat the same with type 'Village'.
0

Featured Post

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Should localization be done inside spring controller 5 25
MySQL  on Tomcat 8 44
hibernate example issues from command prompt 10 40
login form jsp example 2 24
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question