JAXB属性作为元素值

时间:2017-05-22 21:46:17

标签: java xml jaxb

我有定义为

的XML
import 'package:flutter/material.dart';

void main() {
  runApp(new MyApp());
}

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Navigation Example',
      home: new MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key}) : super(key: key);

  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

const List<String> tabNames = const<String>[
  'foo', 'bar', 'baz', 'quox', 'quuz', 'corge', 'grault', 'garply', 'waldo'
];

class _MyHomePageState extends State<MyHomePage> {

  int _screen = 0;

  @override
  Widget build(BuildContext context) {
    return new DefaultTabController(
      length: tabNames.length,
      child: new Scaffold(
        appBar: new AppBar(
          title: new Text('Navigation example'),
        ),
        body: new TabBarView(
          children: new List<Widget>.generate(tabNames.length, (int index) {
            switch (_screen) {
              case 0: return new Center(
                child: new Text('First screen, ${tabNames[index]}'),
              );
              case 1: return new Center(
                child: new Text('Second screen'),
              );
            }
          }),
        ),
        bottomNavigationBar: new Column(
          mainAxisSize: MainAxisSize.min,
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: [
            new AnimatedCrossFade(
              firstChild: new Material(
                color: Theme
                  .of(context)
                  .primaryColor,
                child: new TabBar(
                  isScrollable: true,
                  tabs: new List.generate(tabNames.length, (index) {
                    return new Tab(text: tabNames[index].toUpperCase());
                  }),
                ),
              ),
              secondChild: new Container(),
              crossFadeState: _screen == 0
                              ? CrossFadeState.showFirst
                              : CrossFadeState.showSecond,
              duration: const Duration(milliseconds: 300),
            ),
            new BottomNavigationBar(
              currentIndex: _screen,
              onTap: (int index) {
                setState(() {
                  _screen = index;
                });
              },
              items: [
                new BottomNavigationBarItem(
                  icon: new Icon(Icons.airplanemode_active),
                  title: new Text('Airplane'),
                ),
                new BottomNavigationBarItem(
                  icon: new Icon(Icons.motorcycle),
                  title: new Text('Motorcycle'),
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}

有没有什么方法可以将VALUE属性作为元素的值拉出,而不是将CHILD1作为具有VALUE属性的ComplexType处理,以便它适合这个pojo?

<ROOT>
    <CHILD1 VALUE=""/>
    <CHILD2 VALUE=""/>
</ROOT>

2 个答案:

答案 0 :(得分:0)

JAXB中有一些cutomiziation绑定功能:https://docs.oracle.com/javase/tutorial/jaxb/intro/custom.html 但对于我认为不那么重要的事情,这将是相当复杂的。

如果未生成Java Pojos,您只需添加方法即可直接访问子字段,例如: Root.getChild1String()将调用Root.getChild1()。getValue()

或者您可以更改xml架构。

答案 1 :(得分:0)

我最后编写了一个转换器来转换属性以进行反序列化。

@XmlElement(name = "CHILD1")
@XmlJavaTypeAdapter(ValueAdapter.class)
private String child1;

public class ValueAdapter extends XmlAdapter<Object, String> {
    private static String VALUE = "VALUE";
    @Override
    public String unmarshal(Object e) throws Exception {
        if (e instanceof ElementNSImpl && ((ElementNSImpl)e).hasAttribute(VALUE)) {
            return ((ElementNSImpl)e).getAttribute(VALUE);
        }
        return null;
    }

    @Override
    public Object marshal(String s) throws Exception {
        return null;
    }
}