0

For starters, I'm creating some routes using Camel ver 2.15 (in Fuse 6.2.1).

In my route, i'm trying to create a XML from a pojo that was generated using cxf-xjc maven plugin (cxf-xjc read some xsd somewhere then from the xsd, the pojos with jaxb annotations were produced).

The pojos are TempProject and TempProjects.

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(
name = "",
propOrder = {"ecode", "tempName"}
)
@XmlRootElement(
name = "TempProject"
)
public class TempProject implements Serializable {
@XmlElement(
    name = "Ecode",
    required = true
)
protected String ecode;
@XmlElement(
    name = "TempName",
    required = true
)
protected String tempName;

public TempProject() {
}

public String getEcode() {
    return this.ecode;
}

public void setEcode(String value) {
    this.ecode = value;
}

public String getTempName() {
    return this.tempName;
}

public void setTempName(String value) {
    this.tempName = value;
}
}


@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(
name = "",
propOrder = {"tempProjects"}
)
@XmlRootElement(
name = "TempProjects"
)
public class TempProjects implements Serializable {
@XmlElement(
    name = "TempProject",
    required = true
)
protected List<TempProject> tempProjects;

public TempProjects() {
}

public List<TempProject> getTempProjects() {
    if (this.tempProjects == null) {
        this.tempProjects = new ArrayList();
    }

    return this.tempProjects;
}
}

I can generate the xml using this code:

 JAXBContext jaxbContext = JAXBContext.newInstance(new Class[]{TempProjects.class}); 
 jaxbContext.createMarshaller();
 JaxbDataFormat jaxbDataFormat = new JaxbDataFormat(jaxbContext); //import org.apache.camel.converter.jaxb.JaxbDataFormat;

I call

.marshal(jaxbDataFormat)

in my route to effect the marshalling from the pojo to xml.

The generated xml is posted below:

<TempProjects xmlns="http://blah.blah/foo/schema/v2">
<TempProject>
    <Ecode>1</Ecode>
    <TempName>Tempname1</TempName>
</TempProject>
<TempProject>
    <Ecode>2</Ecode>
    <TempName>Tempname2</TempName>
</TempProject>

How can i generate a marshalled xml that will have a namespace like this...

<TempProjects xmlns:myprefix="http://blah.blah/foo/schema/v2">

Reason being why I needed a namespaceprefix is because I plan to split the values (e.g. Ecode) in the xml using xpath and I needed a namespaceprefix to do that (thats what ive researched, i might be wrong).

My planned code in my route is

.marshal(jaxbDataFormat)
.split( xpath("/TempProjects/TempProject/Ecode/text()").namespaces(ns1), 
new ProjectIdsAggregator()) //the above xpath doesn't work because it doesn't have a namespace prefix

//Namespaces ns1 = new Namespaces("myprefix", "http://blah.blah/foo/schema/v2" );

I looked at jaxbDataFormat.setNamespacePrefixRef("myprefix"), but i got an error (org.apache.camel.NoSuchBeanException: No bean could be found in the registry for: myprefix of type: java.util.Map)

I'm actually quite new in the apache camel routing world, so i might be missing some basic stuff.

bencampbell_14
  • 587
  • 2
  • 10
  • 32
  • Your xpath works without .namespaces(ns1) with default xml. Have you some problems when run route without namespaces ? – c0ld Sep 12 '18 at 12:02
  • @c0ld It doesnt work, i can still see the whole xml in the log after the split()... split[XPath: /TempProjects/TempProject/Ecode/text()] --> stop <<< Pattern:InOut, BodyType:byte[], Body: 1 Tempname1 2 Tempname2 – bencampbell_14 Sep 12 '18 at 12:23
  • Inattentively readed =) Answer about jaxb u cant find here https://stackoverflow.com/questions/6895486/jaxb-need-namespace-prefix-to-all-the-elements but better way is answer by @burki – c0ld Sep 12 '18 at 12:57

1 Answers1

3

You don't need to change your XML at all. It is fine.

With the XML you posted and the Namespace declaration you posted, the following XPath works fine to split the XML (as an example) into two TempProject parts:

xpath("/myprefix:TempProjects/myprefix:TempProject").namespaces(ns1)

Because you declared the XML namespace like this:

Namespaces ns1 = new Namespaces("myprefix", "http://blah.blah/foo/schema/v2" )

Your XPath must use the prefix myprefix for all elements:

/myprefix:TempProjects/myprefix:TempProject 
burki
  • 6,741
  • 1
  • 15
  • 31