需求:
有一文件Student.txt ,内部按一定格式记有学生相关信息,包括ID(编号)、NAME(姓名)、SP(姓名首拼)、GENDER(性别)、AGE(年龄)、EMAIL(电子邮件)、MOBILE(手机号码)。
现要求编写程序,能够从此文件读取学生信息,而后排序,将排序后的数据按格式重新写入文件内,排序规则见下面xml文件,其中有排序字段及过滤数据值设定,要求解析此XML文件获得相关排序及过滤数据信息。
<?xml version="1.0" encoding="UTF-8"?>
<SortColumns>
<SortColumn desc="true">
<ColumnName>AGE</ColumnName>
<FilterData>
<DataValue>26</DataValue>
<DataValue>29</DataValue>
</FilterData>
</SortColumn>
<SortColumn desc="false">
<ColumnName>NAME</ColumnName>
<FilterData>
<DataValue>崔隆</DataValue>
<DataValue>上海市</DataValue>
</FilterData>
</SortColumn>
</SortColumns>

解决方案 »

  1.   

    1. 创建Student类
    2. Student类的排序.
      

  2.   

    先记下咯
    正在学习XML中...
      

  3.   

    也可以考虑用velocity或freeer这类的模板引擎来实现。
      

  4.   

    xml文件<?xml version="1.0" encoding="UTF-8"?>
    <!--<!DOCTYPE books SYSTEM "books.dtd">-->
    <books>  
    <book id="B001" isbn="1092993"  country="cn"
     whflag="true">
    <name>Java面向对象</name>
    <price>35.0</price>
    <nullnode/>

    <pub>
    <name>清华大学出版社</name>
    </pub>
    <author>
    Lu xun<realname>周树人</realname>
    </author>
    </book>
    <book id="B002" isbn="1092994">
    <name>Java面向对象</name>
    <price>35.0</price>
    <nullnode/>

    <pub>
    <name>清华大学出版社</name>
    </pub>
    <author>
    Lu xun<realname>周树人</realname>
    </author>
    </book>
    </books>
      

  5.   

    对该xml的操作类package util;import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.OutputStream;
    import java.net.URL;
    import java.util.List;import org.jdom.Attribute;
    import org.jdom.Document;
    import org.jdom.Element;
    import org.jdom.JDOMException;
    import org.jdom.input.SAXBuilder;
    import org.jdom.output.XMLOutputter;
    import org.jdom.xpath.XPath;import entity.Book;public class DaoBase {

    public static Document getDocument(String fileName){
    SAXBuilder sb = new SAXBuilder();
    URL url = ClassLoader.getSystemResource(fileName);
    try {
    return sb.build(url);
    } catch (JDOMException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return null;
    }

    public void flushDocument(Document doc,String fileName){

    List ls = doc.getRootElement().getChildren();
    for(Object obj : ls){
    Element em = (Element)obj;
    if(Utils.isNullStr(em.getAttributeValue("id"))){
    throw new RuntimeException("Attribute [ id ] can't be null!");
    }
    }

    XMLOutputter xmlOutput = new XMLOutputter();
    URL url = ClassLoader.getSystemResource(".");
    try {
    OutputStream ops = new FileOutputStream(url.getFile()+fileName);
    xmlOutput.output(doc, ops);
    ops.flush();
    ops.close();
    } catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    System.out.println("保存文件成功!");
    }

    public Element Book2Element(Book book){
    Element b = new Element("book");
    if(!Utils.isNullStr(book.getId())){
    b.setAttribute(new Attribute("id",book.getId(),1));
    }
    if(!Utils.isNullStr(book.getIsbn())){
    b.setAttribute(new Attribute("isbn", book.getIsbn(),1));
    }
    if(!Utils.isNullStr(book.getCountry())){
    b.setAttribute(new Attribute("country", book.getCountry(),1));
    }
    if(!Utils.isNullStr(book.getWhflag())){
    b.setAttribute(new Attribute("whflag", book.getWhflag().toString(),1));
    }
    if(!Utils.isNullStr(book.getName())){
    b.addContent(new Element("name").setText(book.getName()));
    }
    if(!Utils.isNullStr(book.getPrice())){
    b.addContent(new Element("price").setText(book.getPrice().toString()));
    }

    b.addContent(new Element("nullnode").setText(null));

    if(!Utils.isNullStr(book.getPubname())){
    b.addContent(new Element("pub")
    .addContent(new Element("name").setText(book.getPubname())));
    }
    if(!Utils.isNullStr(book.getAuthor())){
    if(!Utils.isNullStr(book.getRealname())){
    b.addContent(new Element("author").setText(book.getAuthor())
    .addContent(new Element("realname").setText(book.getRealname())));
    }else{
    b.addContent(new Element("author").setText(book.getAuthor()));
    }
    }
    return b;
    }

    public Book Element2Book(Element book){
    Book b = new Book();
    if(!Utils.isNullStr(book.getAttributeValue("id"))){
    b.setId(book.getAttribute("id").getValue());
    }
    if(!Utils.isNullStr(book.getAttributeValue("isbn"))){
    b.setIsbn(book.getAttributeValue("isbn"));
    }
    if(!Utils.isNullStr(book.getAttributeValue("country"))){
    b.setCountry(book.getAttributeValue("country"));
    }
    if(!Utils.isNullStr(book.getAttributeValue("whflag"))){

    //b.setWhflag(book.getAttributeValue("whflag").equals("true"));
    b.setWhflag(Boolean.valueOf(book.getAttributeValue("whflag")));
    }
    if(!Utils.isNullStr(book.getChildText("name"))){
    b.setName(book.getChild("name").getText());
    }
    if(!Utils.isNullStr(book.getChildText("price"))){
    b.setPrice(Float.valueOf(book.getChildText("price")));
    }

    if(!Utils.isNullStr(book.getChild("pub"))){
    if(!Utils.isNullStr(book.getChild("pub").getChildText("name"))){
    b.setPubname(book.getChild("pub").getChildText("name"));
    }
    }
    if(!Utils.isNullStr(book.getChildText("author"))||!Utils.isNullStr(book.getChildText("realname"))){
    if(Utils.isNullStr(book.getChildText("author"))&&!Utils.isNullStr(book.getChild("author").getChildText("realname"))){
    b.setRealname(book.getChild("author").getChildText("realname"));

    }else if(!Utils.isNullStr(book.getChildText("author"))&&!Utils.isNullStr(book.getChild("author").getChildText("realname"))){
    b.setAuthor(book.getChildText("author"));
    b.setRealname(book.getChild("author").getChildText("realname"));

    }else if(!Utils.isNullStr(book.getChildText("author"))&&Utils.isNullStr(book.getChild("author").getChildText("realname"))){
    b.setAuthor(book.getChildText("author"));
    }
    }
    return b;
    }


      

  6.   


    public List queryByBookFromXml(Document doc,Element book){

    String str = "book";

    if(!Utils.isNullStr(book.getAttributeValue("id"))){
    str = str + "[contains(@id,"+book.getAttribute("id").getValue()+")]";
    }
    if(!Utils.isNullStr(book.getAttributeValue("isbn"))){
    str = str + "[contains(@isbn,"+book.getAttributeValue("isbn")+")]";
    }
    if(!Utils.isNullStr(book.getAttributeValue("country"))){
    str = str + "[contains(@country,"+book.getAttributeValue("country")+")]";
    }
    if(!Utils.isNullStr(book.getAttributeValue("whflag"))){
    str = str + "[contains(@whflag,"+book.getAttributeValue("whflag")+")]";
    }
    if(!Utils.isNullStr(book.getChildText("name"))){
    str = str + "/name[contains(text(),'"+book.getChild("name").getText()+"')]/..";
    }
    if(!Utils.isNullStr(book.getChildText("price"))){
    str = str + "/price[contains(text(),'"+book.getChildText("price")+"')]/..";
    }
    if(!Utils.isNullStr(book.getChild("pub"))){
    if(!Utils.isNullStr(book.getChild("pub").getChildText("name"))){
    str = str + "/pub/name[contains(text(),'"+book.getChild("pub").getChildText("name")+"')]/../..";
    }
    }
    if(!Utils.isNullStr(book.getChildText("author"))||!Utils.isNullStr(book.getChildText("realname"))){
    if(Utils.isNullStr(book.getChildText("author"))&&!Utils.isNullStr(book.getChild("author").getChildText("realname"))){
    str = str + "/author/realname[contains(text(),'"+book.getChild("author").getChildText("realname")+"')]/../..";

    }else if(!Utils.isNullStr(book.getChildText("author"))&&!Utils.isNullStr(book.getChild("author").getChildText("realname"))){
    str = str + "/author[contains(text(),'"+book.getChildText("author")+"')]"
    +"/realname[contains(text(),'"+book.getChild("author").getChildText("realname")+"')]/../..";

    }else if(!Utils.isNullStr(book.getChildText("author"))&&Utils.isNullStr(book.getChild("author").getChildText("realname"))){
    str = str + "/author[contains(text(),'"+book.getChildText("author")+"')]/..";
    }
    }

    System.out.println("queryByBookFromXml : "+str);

    String jdomql = str;
    try {
    List list = XPath.selectNodes(doc.getRootElement(), str);
    return list;
    } catch (JDOMException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return null;
    }

    public List getAllBookFromXml(Document doc){

    String str = "book";

    System.out.println("getAllBookFromXml : "+str);

    String jdomql = str;
    try {
    List list = XPath.selectNodes(doc.getRootElement(), str);
    return list;
    } catch (JDOMException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    return null;
    }

    public void update(Document doc,Element bk){
    try {

    Element book = (Element)XPath.selectSingleNode(
    doc.getRootElement(), "book[@id = '"+bk.getAttributeValue("id")+"']");

    if(!Utils.isNullStr(bk.getAttributeValue("isbn"))){
    book.setAttribute("isbn",bk.getAttributeValue("isbn"));
    }
    if(!Utils.isNullStr(bk.getAttributeValue("country"))){
    book.setAttribute("country",bk.getAttributeValue("country"));
    }
    if(!Utils.isNullStr(bk.getAttributeValue("whflag"))){
    book.setAttribute("whflag",bk.getAttributeValue("whflag"));
    }
    if(!Utils.isNullStr(bk.getChildText("name"))){
    book.getChild("name").setText(bk.getChildText("name"));
    }
    if(!Utils.isNullStr(bk.getChildText("price"))){
    book.getChild("price").setText(bk.getChildText("price"));
    }
    if(!Utils.isNullStr(bk.getChild("pub"))){
    if(!Utils.isNullStr(bk.getChild("pub").getChildText("name"))){
    book.getChild("pub").getChild("name").setText(bk.getChild("pub").getChildText("name"));
    }
    }
    if(!Utils.isNullStr(bk.getChildText("author"))||!Utils.isNullStr(bk.getChildText("realname"))){
    if(Utils.isNullStr(bk.getChildText("author"))&&!Utils.isNullStr(bk.getChild("author").getChildText("realname"))){
    book.getChild("author").getChild("realname").setText(bk.getChild("author").getChildText("realname"));

    }else if(!Utils.isNullStr(bk.getChildText("author"))&&!Utils.isNullStr(bk.getChild("author").getChildText("realname"))){

    book.getChild("author").setText(bk.getChildText("author"));
    book.getChild("author").addContent(new Element("realname"));
    book.getChild("author").getChild("realname").setText(bk.getChild("author").getChildText("realname"));

    }else if(!Utils.isNullStr(bk.getChildText("author"))&&Utils.isNullStr(bk.getChild("author").getChildText("realname"))){
    book.getChild("author").setText(bk.getChildText("author"));
    }
    }
    } catch (JDOMException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }
    }
    }
      

  7.   

    LZ 的意思看懂了
    但是你的XML 没看到哪里是排序字段及过滤数据值设定
      

  8.   

    涉及到xml文件的读写,如果楼主这方面没问题,就好办多了,对于student的排序,定义student类,定好排序规则,放在集合中,然后再从集合中写到xml中。