Web / Apache Axiom Interview questions
How do you create an OMElement using OMFactory?
Creating an element by hand is a matter of getting a factory, creating a namespace if the element needs one, then calling one of the createOMElement overloads.
OMFactory factory = OMAbstractFactory.getOMFactory(); OMNamespace ns = factory.createOMNamespace("http://example.com/catalog", "cat"); OMElement product = factory.createOMElement("Product", ns); product.addAttribute(factory.createOMAttribute("sku", null, "SKU-100")); OMElement name = factory.createOMElement("Name", ns); name.setText("Wireless Mouse"); product.addChild(name);
Note that attributes are typically created without a namespace (passing null) unless the schema specifically requires a namespaced attribute, which is a common beginner mistake when porting DOM code over to Axiom.
Once built this way, the element behaves exactly like one produced by parsing - it can be serialized, added to a SOAP body, or passed to any API expecting an OMElement.
More Related questions...