简体   繁体   English

如何将 XML 文件中的 x 和 y 坐标解析为 Java 中的 Point2D 数组?

[英]How can I parse x and y coordinates from a XML file into an array of Point2D in Java?

I'm currently facing the problem that I want to feed the x and y coordinates from an XML into Java and I'm not sure how to connect the individual coordinate pairs (x and y) to points and in what form of array I should store them?我目前面临的问题是,我想将 XML 中的 x 和 y 坐标输入 Java 并且我不确定如何将各个坐标对(x 和 y)连接到点以及我应该以哪种形式的数组存储它们? The number of points is not predefined so I guess I should use an ArrayList?.点数没有预定义,所以我想我应该使用 ArrayList?。 Further, coordinates can also be integers sometimes (not like in the example below).此外,坐标有时也可以是整数(不像下面的例子)。 My question is how to include reading into the array directly into parsing the XML file.我的问题是如何在解析 XML 文件时直接读取数组。 Below are some first code fragments I tried..下面是我尝试的一些第一个代码片段..

The XML File looks like this: XML 文件如下所示:

<?xml version="1.0" encoding="UTF-8" standalone="no"?><Room><ID>Test</ID><corners><corner><x>400.3</x><y>997.2</y></corner><corner><x>400.3</x><y>833.1</y></corner><corner><x>509.9</x><y>833.1</y></corner><corner><x>509.9</x><y>700.0</y></corner>...<corner><x>1012.1</x><y>500.8</y></corner><corner><x>1012.1</x><y>900.2</y></corner><corner><x>902.0</x><y>900.2</y></corner><corner><x>902.0</x><y>997.2</y></corner></corners></Room>

My XMLParser class looks like this:我的 XMLParser class 看起来像这样:

package parser;

import data.Room;
import parser.exc.XMLTransfererException;

import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.filter.ElementFilter;
import org.jdom2.input.SAXBuilder;
import org.jdom2.input.sax.XMLReaders
import org.jdom2.util.IteratorIterable;

import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;


public class XMLTransferer implements IXMLTransferer {

final String dtdName = "DataModel.dtd";
final String dtdPath = "../File_Processing_Component/Model";
final String dtdFilename = String.format("%s/%s", dtdPath, dtdName);

final String xmlDoctypeRootElementName ="Raum";
boolean isValid;

SAXBuilder builder = new SAXBuilder(XMLReaders.NONVALIDATING);
Document doc = builder.build(new File(xmlFilename));

doc.setDocType(new DocType(xmlDoctypeRootElementName, dtdFilename));
String xmlString = new XMLOutputter().outputString(doc);

SAXBuilder validationBuilder = new SAXBuilder(XMLReaders.DTDVALIDATING);

@Override
public Room readFromFile(String dtdPath)
    throws XMLTransfererException {
try (doc = validationBuilder.build(new StringReader(xmlString));)
isValid = true;
} catch (JDOMException jdEx) {
// ...
isValid = false;
}
if (isValid) 
try {  IteratorIterable<Element> iter = root.getDescendants(new ElementFilter("corner"));
while (iter.hasNext()) {
    Element elem = iter.next();
    Element childX = elem.getChild("x");
    array.getPoints().add(Double.parseDouble(childX.getText()));
    Element childY = elem.getChild("y");
    array.getPoints().add(Double.parseDouble(childY.getText()));
}
}
catch (IOException | JDOMException x) {
x.printStackTrace();
}
System.out.println(array);
}
}

As you can think this is far beyond complete, however while searching an answer I don't find a way how to manage this properly.正如您可能认为的那样,这远远不够完整,但是在搜索答案时,我找不到如何正确管理它的方法。 Help much appreciated!非常感谢帮助!

try with following solution,尝试以下解决方案,

declare the two dimensional array list as follows,如下声明二维数组列表,

List<List<Double>> listCoordinate = new ArrayList<List<Double>>();

add the coordinate values pare (x and y) as a list into listCoordinate,将坐标值 pare (x 和 y) 作为列表添加到 listCoordinate 中,

listCoordinate.add(Arrays.asList(Double.parseDouble(childX.getText()), Double.parseDouble(childY.getText()));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM