How to use constants for XML

How to define java constants like Integer.MAX_VALUE in XML? I know how to use enums, but I have a third-party library that must use constant operations.
For example. There are some values ​​in the xml file, and I think it should be declared as a constant in the generated java class.

XML:


Joe
Walnes

123
1234-456


123
9999-999

Java:

public class Person {
private String firstname;
private String lastname;
private PhoneNumber phone;
private PhoneNumber fax;
// ... constructors and methods
}

public class PhoneNumber {
private int code;
private String number;
//. .. constructors and methods
}

This works. But it should look like this:

XML:

< person>
Joe
Walnes

****PnoneNumber.LOCAL**< /const>**


123
9999-999

Java should be:

public class Person {
private String firstname;
private String lastname;
private PhoneNumber phone;
private PhoneNumber fax;
// ... constructors and methods
}

public class PhoneNumber {
public static final PnoneNumber LOCAL=new PhoneNumber(123,"1234-456");
private int code;
private String number;
// ... constructors and methods
}

Can I do this easily without a custom converter?

Thank you very much.

I have studied several XML-POJO generation To my surprise, the generators, especially those that use XML Schema (XSD) to define classes, do not provide the ability to modify attributes.

If you want to keep the current solution, I think the most concise way is to extend the conversion with your own custom converter, as you said yourself.

If you are using something that uses XML Schema, you can always use attributes fixed = “constant” as standard initialization, but of course it does not retain semantics.

However, it might be better to solve this problem in another way. These are constants and they should not be changed, so maybe It is better to define them in a separate file.

Excuse me, how to define java constants like Integer.MAX_VALUE in XML? I know how to use enums, but I have a third-party library that must use constant operations.
For example. There are some values ​​in the xml file, and I think it should be declared as a constant in the generated java class.

XML:


Joe
Walnes

123
1234-456


123
9999-999

Java:

public class Person {
private String firstname;
private String lastname;
private PhoneNumber phone;
private PhoneNumber fax;
// ... constructors and methods
}

public class PhoneNumber {
private int code;
private String number;
//. .. constructors and methods
}

This works. But it should look like this:

XML:

< person>
Joe
Walnes

****PnoneNumber.LOCAL**< /const>**


123
9999-999

Java should be:

 public class Person {
private String firstname;
private String lastname;
private PhoneNumber phone;
private PhoneNumber fax;
// ... constructors and methods
}

public class PhoneNumber {
public static final PnoneNumber LOCAL=new PhoneNumber(123,"1234-456");
private int code;
private String number;
// ... constructors and methods
}

Can I do this easily without a custom converter?

Thank you very much.

I have studied several XML-POJO generators, especially those that use XML Schema (XSD) to The generator that defines the class, to my surprise, does not provide the ability to modify attributes.

If you want to keep the current solution, I think the most concise way is through your own Define the converter to extend the conversion, as you said yourself.

If you are using something that uses XML Schema, you can always use the attribute fixed=”constant” as the standard initialization, but of course it The semantics are not preserved.

However, it might be better to solve this problem in another way. These are constants and they should not be changed, so maybe it is better to define them in a separate file.

Leave a Comment

Your email address will not be published.