0

I am using java SimpleXML to parse XML from a number of applications.

Many applications create quirky XML implementations of this supposed 'standard', such as putting in an 'enabled' tag more than once.

In this situation, I just want to ignore the second one as it is a mistake and has same value as first anyway, but SimpleXML throws an exception "Element 'enabled' is already used"

How do I prevent this?

This is the field that is complained about.

@Element(required = false)
protected boolean enabled = true;

The XML is huge so don't want to post it. Is there a way to get SimpleXML to report the line number that the caused the error?

5
  • can you show an example?
    – wero
    Jul 26, 2015 at 11:59
  • Updated to show the java field annotaion, but massive XML, so would rather track down the exact line number to post a snippet. Is there a way to get the line number?
    – John Baker
    Jul 26, 2015 at 12:19
  • I have another issue too. Even though I am using Root(strict = false) and read(strict=false), simplexml is still being strict. I really want it to ignore problems.
    – John Baker
    Jul 26, 2015 at 12:24
  • Cant u use XSLT first to preprocess the XML to remove duplicated tag and than use SimpleXML to parse it...for removing duplicate elements using XSLT you could follow the link stackoverflow.com/questions/10912544/…
    – Megha
    Jul 27, 2015 at 5:55
  • I will try that next but would prefer first to get SimpleXML to stop being strict with validation. I can't seem to turn it off but unsure why. I would hope that in non strict mode it might simply ignore the second setting of that property. Any ideas why it may ignoring my attempt to turn off strict?
    – John Baker
    Jul 27, 2015 at 10:34

1 Answer 1

1

try to following annotations:

   class RepeatElements {
      @ElementListUnion({
         @ElementList(entry = "enable", 
           type = Boolean.class, inline = true)
        })
      private ArrayList<Boolean> enables = new ArrayList<>();

      public boolean isEnabled() {
        // TODO check size
        return enables.get(0).booleanValue();
      }
    }

ouput for class RepeatElements with some "enables"

    <repeatElements>
        <enable>true</enable>
        <enable>true</enable>
        <enable>true</enable>
    </repeatElements>
3
  • Thanks that would work. I was thinking of doing this as a last resort. But I am still totally confused about why I cannot turn off strict validation. Any ideas about that?
    – John Baker
    Jul 28, 2015 at 18:14
  • I think if you annotate the class attribute enabled with @Element(required=false), simple-framework expects exact one or zero xml elements named "enabled", with strict=false simple-framework skips elements with other names than "enabled", two ore more elements with name "enabled" is an error and could not be skipped Jul 29, 2015 at 7:49
  • here you find a gist gist.github.com/momolinus/5104fa890936d0cd58dd with the sample Jul 29, 2015 at 7:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.