Solved

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

Posted on 2008-10-28
1
1,109 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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
array6 challenfge 6 63
maven example error 3 36
ArrayIndexOutOfBoundException 9 35
Java Server Faces parameter pass? 6 22
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

760 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now