Jump to content

Java CDATA XML Help

Ryujin2003
Go to solution Solved by mackncheesiest,

I'm not incredibly experienced with JAXB, but I believe you may be looking for the @XmlAttribute annotation. I think one solution might be something like

@XmlRootElement(name = "Style")
public class Style {
  
  @XmlElement(name = "IconStyle")
  private IconStyle iconStyle;

  @XmlAttribute(name = "id")
  private String id;
  
  ...
}

I believe that should add an XML attribute to the Style tag (which if I'm interpreting your problem right is what you're looking for), and as long as JAXB's marshalling then properly marshalls the IconStyle tag, I think you'll be good. Also see https://stackoverflow.com/a/7498378

I'm currently working on a project that needs to export data to XML (creating KML file). The long of the short, I need help creating a different header and footer tag in XML....

 

So I have an issue I cannot find a resolution for and am looking for guidance or possible fix. I'm using JABX to make my XML script much easier to handle. So, for my KML, the first section is style. Here is the "generic" GoogleEarth KML:

<Style id="s_ylw-pushpin">
	<IconStyle>
		<scale>1.2</scale>
		<Icon>
			<href>http://maps.google.com/mapfiles/kml/shapes/placemark_circle.png</href>
		</Icon>
	</IconStyle>
</Style>

So, I can correctly push out everything within <IconStyle>; however, the issue I'm having is with the <Style> header. Its different than the footer (header has the id included, footer doesn't).

I haven't been able to find a way in JABX to create a separate header from footer (unless I'm blind and haven't found it yet).

 

My current IconStyle:

 

... (paraphrased)

@XmlRootElement (name = "IconStyle")
@XmlType (propOrder = ...)

public class IconStyle {
	double scale = 1.2;
	String icon = "...insert location url here ...";

	.......
}

I've tried creating a list class to parent this, so I can serialize my  <Style id= "..."> in the output. However, I cannot get that any class using JABX to function, nor manually push in my KML output class the ability to have a different header and footer (syntax issues when trying to use certain characters in the output). JABX makes the header/foot match exactly... I was pointed into the direction of using CDATA, but I am having trouble finding a nice clean example to use. If I could use Character DATA to force the header and footer, I would be completely fine with this; however, no luck as of yet.

 

If JABX has this functionality, it would definitely make the code cleaner since manual would take up space and not look as sleek. However, I'm open to other options as well. Trying to force String output for some reason just doesn't jive well when being output into XML.

 

Any insight, suggestions, or redirection would be much appreciated. Thank you very much in advance.

Link to comment
Share on other sites

Link to post
Share on other sites

I'm not incredibly experienced with JAXB, but I believe you may be looking for the @XmlAttribute annotation. I think one solution might be something like

@XmlRootElement(name = "Style")
public class Style {
  
  @XmlElement(name = "IconStyle")
  private IconStyle iconStyle;

  @XmlAttribute(name = "id")
  private String id;
  
  ...
}

I believe that should add an XML attribute to the Style tag (which if I'm interpreting your problem right is what you're looking for), and as long as JAXB's marshalling then properly marshalls the IconStyle tag, I think you'll be good. Also see https://stackoverflow.com/a/7498378

Link to comment
Share on other sites

Link to post
Share on other sites

5 hours ago, mackncheesiest said:

I'm not incredibly experienced with JAXB, but I believe you may be looking for the @XmlAttribute annotation. I think one solution might be something like


@XmlRootElement(name = "Style")
public class Style {
  
  @XmlElement(name = "IconStyle")
  private IconStyle iconStyle;

  @XmlAttribute(name = "id")
  private String id;
  
  ...
}

I believe that should add an XML attribute to the Style tag (which if I'm interpreting your problem right is what you're looking for), and as long as JAXB's marshalling then properly marshalls the IconStyle tag, I think you'll be good. Also see https://stackoverflow.com/a/7498378

Thanks. That looks very promising. I'll give it a shot, and update accordingly. Thank you! I've been baking my head against a wall since Friday

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×