0

I have an xml file and want to sort like this in txt file

Soccer | England | Premier League | Blackpool FC - Birmingham City 5-1

<?xml version="1.0" encoding="UTF-8"?>
  <LivescoreData>
  <Sport SportId="1">
   <Name language="en">Soccer</Name>
   <Name language="se">Fotboll</Name>
   <Category CategoryId="34">
    <Name language="en">Australia</Name>
   <Name language="se">Australien</Name>
   <Tournament TournamentId="144">
    <Name language="en">Hyundai A-League</Name>
    <Name language="se">Hyundai A-League</Name>
    <Match MatchId="4616735">
      ....
     etc
1
  • 3
    What does "sort like this" mean? What does your XML file look like? Commented Jan 17, 2012 at 14:02

3 Answers 3

0

Use dom4j.It's a great library for working with xml files.
You can download it from here.

    SAXReader reader = new SAXReader();
    Document doc = reader.read(new File("myXML.xml"));
    Element content = doc.getRootElement();
    List<Element> myNeededElements = content.elements("neededElementName");

After this you'll have all elements with the name you want (say "neededElementName") in myNeededElements list.

Sign up to request clarification or add additional context in comments.

Comments

0

If you have flexible options in selecting a library/framework, one of the solutions is to use XStream and deserialize the xml into a collection of java objects and sort 'em.

For Ex:

List<Employee> employeeList = xStream.fromXML(xmlFile);
Collections.sort(employeeList, new Comparator() { .... });

EDIT: Also check the below links:

1) How to sort XML elements in Java?

2) XSLT Sort

Comments

0

Sorting an XML file can generally be done in a few lines of XSLT or XQuery, and invoking XSLT or XQuery only takes a few lines of Java. However, it's totally unclear from your post what your desired input and output are (or at any rate, what relationship they have to each other.)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.