Read xml in Java

Asked

Viewed 659 times

0

I’m using a program called Tiled(Tilemap) to generate a map and I want to read these numbers.

The xml is like this:

<?xml version="1.0" encoding="UTF-8"?>
<map version="1.0" tiledversion="1.0.2" orientation="orthogonal" renderorder="right-down" width="42" height="34" tilewidth="16" tileheight="16" nextobjectid="8">
 <tileset firstgid="1" name="tileset" tilewidth="16" tileheight="16" tilecount="924" columns="33">
  <image source="tileset.png" trans="003bff" width="528" height="448"/>
 </tileset>
 <layer name="background" width="42" height="34">
  <data encoding="csv"> // Apos isso
763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,
763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,
763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,
763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,
763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,
763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,
763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,
763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,
763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,
763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,
763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,
763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,
763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,
763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,
763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,
763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,
763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,
763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,
763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,
763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,
763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,
763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,
763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,
763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,
763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,
763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,
763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,
763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,
763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,
763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,
763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,
763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,
763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,
763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763,763
</data>
 </layer>

Only I haven’t found any way I can do that without copying to another file.

  • https://www.mkyong.com/java/how-to-read-xml-file-in-java-dom-parser/

  • What do you want to do with this data? You can use the JSoup for this. I can make an example if you say exactly what you want to extract from it.

1 answer

1

You can use the Jackson-xml-dataformat

For this just add the dependency in your pom.xml:

<dependency>
  <groupId>com.fasterxml.jackson.dataformat</groupId>
  <artifactId>jackson-dataformat-xml</artifactId>
  <version>2.7.4</version>
</dependency>

You can define the Beans according to the structure of your XML:

tag date

@JacksonXmlRootElement(localName="data")
public class XmlData {

  @JacksonXmlProperty(isAttribute = true)
  private String encoding;

  @JacksonXmlText
  private String value;

  public String getEncoding() {
    return encoding;
  }

  public void setEncoding(String encoding) {
    this.encoding = encoding;
  }

  public String getValue() {
    return value;
  }

  public void setValue(String value) {
    this.value = value;
  }

 }

tag layer

@JacksonXmlRootElement(localName = "layer")
public class XmlLayer {

  @JacksonXmlProperty(isAttribute = true)
  private String name;

  @JacksonXmlProperty(isAttribute = true)
  private String width;

  @JacksonXmlProperty(isAttribute = true)
  private String height;

  @JacksonXmlProperty
  private XmlData data;

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getWidth() {
    return width;
  }

  public void setWidth(String width) {
    this.width = width;
  }

  public String getHeight() {
    return height;
  }

  public void setHeight(String height) {
    this.height = height;
  }

  public XmlData getData() {
    return data;
  }

  public void setData(XmlData data) {
    this.data = data;
  }

}

tag image

@JacksonXmlRootElement(localName = "image")
public class XmlImage {

  @JacksonXmlProperty(isAttribute = true)
  private String source;

  @JacksonXmlProperty(isAttribute = true)
  private String trans;

  @JacksonXmlProperty(isAttribute = true)
  private String width;

  @JacksonXmlProperty(isAttribute = true)
  private String height;

  public String getSource() {
    return source;
  }

  public void setSource(String source) {
    this.source = source;
  }

  public String getTrans() {
    return trans;
  }

  public void setTrans(String trans) {
    this.trans = trans;
  }

  public String getWidth() {
    return width;
  }

  public void setWidth(String width) {
    this.width = width;
  }

  public String getHeight() {
    return height;
  }

  public void setHeight(String height) {
    this.height = height;
  }

}

tag tileset

@JacksonXmlRootElement(localName = "tileset")
public class XmlTileSet {

  @JacksonXmlProperty(isAttribute = true, localName = "firstgid")
  private String firstGid;

  @JacksonXmlProperty(isAttribute = true, localName = "name")
  private String name;

  @JacksonXmlProperty(isAttribute = true, localName = "tilewidth")
  private String tileWidth;

  @JacksonXmlProperty(isAttribute = true, localName = "tileheight")
  private String tileHeight;

  @JacksonXmlProperty(isAttribute = true, localName = "tilecount")
  private String tileCount;

  @JacksonXmlProperty(isAttribute = true, localName = "columns")
  private String columns;

  @JacksonXmlProperty(isAttribute = true)
  private XmlImage image;

  public String getFirstGid() {
    return firstGid;
  }

  public void setFirstGid(String firstGid) {
    this.firstGid = firstGid;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public String getTileWidth() {
    return tileWidth;
  }

  public void setTileWidth(String tileWidth) {
    this.tileWidth = tileWidth;
  }

  public String getTileHeight() {
    return tileHeight;
  }

  public void setTileHeight(String tileHeight) {
    this.tileHeight = tileHeight;
  }

  public String getTileCount() {
    return tileCount;
  }

  public void setTileCount(String tileCount) {
    this.tileCount = tileCount;
  }

  public String getColumns() {
    return columns;
  }

  public void setColumns(String columns) {
    this.columns = columns;
  }

  public XmlImage getImage() {
    return image;
  }

  public void setImage(XmlImage image) {
    this.image = image;
  }

}

tag map

@JacksonXmlRootElement(localName = "map")
public class XmlMap {

  @JacksonXmlProperty(isAttribute = true, localName = "version")
  private String version;

  @JacksonXmlProperty(isAttribute = true, localName = "tiledversion")
  private String tiledVersion;

  @JacksonXmlProperty(isAttribute = true, localName = "orientation")
  private String orientation;

  @JacksonXmlProperty(isAttribute = true, localName = "renderorder")
  private String renderOrder;

  @JacksonXmlProperty(isAttribute = true)
  private String width;

  @JacksonXmlProperty(isAttribute = true)
  private String height;

  @JacksonXmlProperty(isAttribute = true, localName = "tilewidth")
  private String tileWidth;

  @JacksonXmlProperty(isAttribute = true, localName = "tileheight")
  private String tileHeight;

  @JacksonXmlProperty(isAttribute = true, localName = "nextobjectid")
  private String nextObjectId;

  @JacksonXmlProperty(localName = "tileset")
  private XmlTileSet xmlTileSet;

  @JacksonXmlProperty(localName = "layer")
  private XmlLayer xmlLayer;

  public String getVersion() {
    return version;
  }

  public void setVersion(String version) {
    this.version = version;
  }

  public String getTiledVersion() {
    return tiledVersion;
  }

  public void setTiledVersion(String tiledVersion) {
    this.tiledVersion = tiledVersion;
  }

  public String getOrientation() {
    return orientation;
  }

  public void setOrientation(String orientation) {
    this.orientation = orientation;
  }

  public String getRenderOrder() {
    return renderOrder;
  }

  public void setRenderOrder(String renderOrder) {
    this.renderOrder = renderOrder;
  }

  public String getWidth() {
    return width;
  }

  public void setWidth(String width) {
    this.width = width;
  }

  public String getHeight() {
    return height;
  }

  public void setHeight(String height) {
    this.height = height;
  }

  public String getTileWidth() {
    return tileWidth;
  }

  public void setTileWidth(String tileWidth) {
    this.tileWidth = tileWidth;
  }

  public String getTileHeight() {
    return tileHeight;
  }

  public void setTileHeight(String tileHeight) {
    this.tileHeight = tileHeight;
  }

  public String getNextObjectId() {
    return nextObjectId;
  }

  public void setNextObjectId(String nextObjectId) {
    this.nextObjectId = nextObjectId;
  }

  public XmlTileSet getXmlTileSet() {
    return xmlTileSet;
  }

  public void setXmlTileSet(XmlTileSet xmlTileSet) {
    this.xmlTileSet = xmlTileSet;
  }

  public XmlLayer getXmlLayer() {
    return xmlLayer;
  }

  public void setXmlLayer(XmlLayer xmlLayer) {
    this.xmlLayer = xmlLayer;
  }

}

Reading the xml:

XmlMapper mapper = new XmlMapper();
XmlMap data = mapper.readValue(new File("/direrotio/arquivo.xml"), XmlMap.class);
System.out.println(data.getXmlLayer().getData().getValue());

the input of the readValue method can be byte array, Inputstream and etc, just take a look at the documentation.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.