有没有一种推荐的方式可以获取所有信息?

huangapple go评论80阅读模式
英文:

Is there a recommended way in order to have all information?

问题

我打算在Java中读取XML文件(多边形的坐标),并希望直接以适当的方式表示多边形,以解决几何问题。这是我应该在主方法中做的吗?

要读取XML文件,我创建了以下代码:

public class ReadDataFromXmlFile throws IOException, SAXException, 
ParserConfigurationException {

    public static void main (String[] args) {
        File fXmlFile = new File("/Users/instances/test_coordinates.xml");
        GeometryFactory geometryFactory = new GeometryFactory();
        Polygon polygonFromCoordinates = geometryFactory.createPolygon(coordinates);
    }
}

然而,我的问题是,多边形将如何表示。我能访问x和y坐标吗?

也许有人可以提供一些解释?

谢谢!

英文:

I aim to read in a XML file into Java (the coordinates of a polygon). And directly want to represent the polygon in a proper way to tackle a geometric question. Is this something I should do in the main method?

For reading in the XML file I created this code:

    public class ReadDataFromXmlFile throws IOException, SAXException, 
    ParserConfigurationException {

    public static void main (String[] args) {
      File fXmlFile = new File("/Users/instances/test_coordinates.xml");
      GeometryFactory geometryFactory = new GeometryFactory();
      Polygon polygonFromCoordinates = geometryFactory.createPolygon(coordinates);
     }

However, my question is, how the polygon will be represented. Can I access x and y coordinates?

Maybe someone can give some clarification?

Thanks!

答案1

得分: 1

根据我的观点,从XML文件中读取坐标的正确方法是使用String方法,如find()、split()等。关于在main函数中执行:当然是在一个单独的方法中进行。如果您想要读取另一个文件呢?不要忘记使用try-catch!!

public static Polygon getPolygonFromFile(String filePath) {
    File fXmlFile = null;
    Polygon resultPolygon = null;
    try {
        // 在此处检查文件是否存在
        fXmlFile = new File(filePath);
    } catch (Exception e) {
        // 如果文件不存在,应该做什么处理
    }
    // 执行您需要的操作以创建Polygon
    return resultPolygon;
}
英文:

In my opinion, the proper way to read coordinates from an XML file is using String methods like find(), split(), etc. About doing it in main: of course in a separate method. What if you would like to read another file? And don't forget to use a try-catch!!

public static Polygon getPolygonFromFile(String filePath)
    File fXmlFile = null;
    Polygon resultPolygon = null; 
    try {
    // check here whether the file exists
    fXmlFile = new File(filePath);
    } catch (Exception e) {
    // what to do if the file doesn't exist
    }
    // do what you need to create your Polygon
    return resultPolygon;
}

huangapple
  • 本文由 发表于 2020年4月7日 18:04:09
  • 转载请务必保留本文链接:https://go.coder-hub.com/61077480.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定