现在可以构造包装器元素 line-item 了:
OMElement lineItem= factory.createOMElement("line-item", poNs);
接下来创建 line-item 元素相关的子元素和属性。
最好用下面的方式创建元素属性:
lineItem.addAttribute("quantity", "2", poNs);
与其他元素一样创建子元素,然后按照下面的方式结合到父元素中:
OMElement description= factory.
createOMElement("description", poNs);
description.setText("Burnham's Celestial Handbook, Vol 2");
lineItem.addChild(description);
类似地,也添加 price 子元素:
OMElement price= factory.createOMElement("price", poNs);
price.setText("19.89");
lineItem.addChild(price);
清单 2 显示了完整的代码片段。
清单 2.通过程序创建 line item
OMFactory factory = OMAbstractFactory.getOMFactory();
OMNamespace poNs =
factory.createOMNamespace("http://openuri.org/easypo", "po");
OMElement lineItem =
factory.createOMElement("line-item", poNs);
lineItem.addAttribute("quantity", "2", poNs);
OMElement description =
factory.createOMElement("description", poNs);
description.setText("Burnham's Celestial Handbook, Vol 2");
lineItem.addChild(description);
OMElement price = factory.createOMElement("price", poNs);
price.setText("19.89");
lineItem.addChild(price);
