如何使用Java代码将以下内容转换为MON-FRI

时间:2023-01-18 23:30:30
<Days>
    <day>Mon</day>
     <day>Tue</day>
     <day>Wed</day>
     <day>Thu</day>
     <day>Fri</day>
</Days>

2 个解决方案

#1


0  

Try this:

  File file = new File("YourXMLFile.xml");
  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  DocumentBuilder db = dbf.newDocumentBuilder();
  Document doc = db.parse(file);
  doc.getDocumentElement().normalize();
  NodeList nodeLst = doc.getElementsByTagName("days");

  if (nodeLst.getLenth() > 1) {
     String first = ((Node) nodeLst.item(s)).getNodeValue().toString().toUpperCase();
     String last = ((Node) nodeLst.item(nodeLst.getLength() - 1)).getNodeValue().toString().toUpperCase();
     System.out.println(first + "-" + last);
  }

#2


0  

Here you go an example using xstream library:

在这里,您将使用xstream库进行示例:

package MonFri.monfri;

import java.util.List;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;


public class App 
{
@SuppressWarnings("unchecked")
public static void main( String[] args )
{

    XStream xstream = new XStream(new DomDriver());
    xstream.alias("day", String.class);
    xstream.alias("Days", List.class);
    List<String> days = (List<String>)xstream.fromXML(data);
    String firstDay = days.get(0);
    String lastDay = days.get(days.size()-1);
    System.out.println(firstDay.toUpperCase() + "-" + lastDay.toUpperCase());
}
private static String data = "<Days>\n" + 
        "    <day>Mon</day>\n" + 
        "     <day>Tue</day>\n" + 
        "     <day>Wed</day>\n" + 
        "     <day>Thu</day>\n" + 
        "     <day>Fri</day>\n" + 
        "</Days>";
}

which prints MON-FRI

打印MON-FRI

Hope it helps

希望能帮助到你

#1


0  

Try this:

  File file = new File("YourXMLFile.xml");
  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  DocumentBuilder db = dbf.newDocumentBuilder();
  Document doc = db.parse(file);
  doc.getDocumentElement().normalize();
  NodeList nodeLst = doc.getElementsByTagName("days");

  if (nodeLst.getLenth() > 1) {
     String first = ((Node) nodeLst.item(s)).getNodeValue().toString().toUpperCase();
     String last = ((Node) nodeLst.item(nodeLst.getLength() - 1)).getNodeValue().toString().toUpperCase();
     System.out.println(first + "-" + last);
  }

#2


0  

Here you go an example using xstream library:

在这里,您将使用xstream库进行示例:

package MonFri.monfri;

import java.util.List;
import com.thoughtworks.xstream.XStream;
import com.thoughtworks.xstream.io.xml.DomDriver;


public class App 
{
@SuppressWarnings("unchecked")
public static void main( String[] args )
{

    XStream xstream = new XStream(new DomDriver());
    xstream.alias("day", String.class);
    xstream.alias("Days", List.class);
    List<String> days = (List<String>)xstream.fromXML(data);
    String firstDay = days.get(0);
    String lastDay = days.get(days.size()-1);
    System.out.println(firstDay.toUpperCase() + "-" + lastDay.toUpperCase());
}
private static String data = "<Days>\n" + 
        "    <day>Mon</day>\n" + 
        "     <day>Tue</day>\n" + 
        "     <day>Wed</day>\n" + 
        "     <day>Thu</day>\n" + 
        "     <day>Fri</day>\n" + 
        "</Days>";
}

which prints MON-FRI

打印MON-FRI

Hope it helps

希望能帮助到你