英文:
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;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论