Set multiple namespace with different prefix using jdom - java

I would like to have this format in xml
<?xml version="1.0" encoding="UTF-8"?>
<Document xmlns="urn:001.003.03" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="urn:tech:xsd:pain.001.001.03">
<staff id="1">
<firstname>yong</firstname>
</staff>
</Document>
I am using the following code.
Namespace namespace = Namespace.getNamespace("urn:001.003.03");
Namespace namespace1 = Namespace.getNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
Namespace namespace2 = Namespace.getNamespace("schemaLocation", "urn:tech:xsd:pain.001.001.03");
Element document = new Element("Document", namespace);
Document doc = new Document(document);
doc.setRootElement(document);
document.addNamespaceDeclaration(namespace1);
document.addNamespaceDeclaration(namespace2);
Element staff = new Element("staff", namespace);
staff.setAttribute(new Attribute("id", "1"));
staff.addContent(new Element("firstname",namespace).setText("yong"));
doc.getRootElement().addContent(staff);
I am getting output is:
<?xml version="1.0" encoding="UTF-8"?>
<Document xmlns="urn:001.003.03" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:schemaLocation="urn:tech:xsd:pain.001.001.03">
<staff id="1">
<firstname>yong</firstname>
</staff>
</Document>
I need namespace like xsi:schemaLocation but its generating namespace like xmlns:schemaLocation

Related

Multiple values to a single index

I stored the data from a csv in String[] but single index contains many values
For Example:- a[0] hasJohn Smith Jane Doe Jaan Lepp FooBar. So when I am parsing it to XML it creates 4 context for these 4 values. How can I pass all the name in single athletes node?
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Athletes>
<Name>John Smith</Name>
<Athletes>
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<Athletes>
<Name>Jane Doe</Name>
<Athletes>

XML attributes order is changing after DOM processing using Java code

I am trying to read an XML and writing into another XML by changing some attribute values When processing XML by means of standard DOM, attribute order is changing.
Here I am trying to change the 'id' attribute value of 'staff' element.
input xml:
<company xyz="xyz" def="def">
<staff id="1" def="def" xyz="xyz" abc="abc">
<firstname>yong</firstname>
<lastname>mook kim</lastname>
<nickname>mkyong</nickname>
<salary>100000</salary>
</staff>
</company>
output xml:
<company def="def" xyz="xyz">
<staff abc="abc" def="def" id="2" xyz="xyz">
<firstname>yong</firstname>
<lastname>mook kim</lastname>
<nickname>mkyong</nickname>
<salary>100000</salary>
</staff>
</company>
Expecting output xml:
<company xyz="xyz" def="def">
<staff id="2" def="def" xyz="xyz" abc="abc">
<firstname>yong</firstname>
<lastname>mook kim</lastname>
<nickname>mkyong</nickname>
<salary>100000</salary>
</staff>
</company>
Could someone please help with the source code.
Waiting for the responses.

xml schema in jaxb java not getting expected output

My expected output is in this format of JAXB.
<?xml version="1.0" encoding="utf-8"?>
<req:ShipmentRequest xmlns:req="http://www.dhl.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.dhl.com ship-val-global-req.xsd" schemaVersion="5.0">
<Request>
<ServiceHeader>
<MessageTime>2001-12-17T09:30:47-05:00</MessageTime>
<MessageReference>1234567890123456789012345678901</MessageReference>
<SiteID>CustomerTest</SiteID>
<Password>alkd89nBV</Password>
</ServiceHeader>
</req:ShipmentRequest>
but i am able to get only
<?xml version="1.0" encoding="utf-8"?>
<ShipmentRequest xmlns:req="http://www.dhl.com" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.dhl.com ship-val-global-req.xsd" schemaVersion="5.0">
<Request>
<ServiceHeader>
<MessageTime>2001-12-17T09:30:47-05:00</MessageTime>
<MessageReference>1234567890123456789012345678901</MessageReference>
<SiteID>CustomerTest</SiteID>
<Password>alkd89nBV</Password>
</ServiceHeader>
</ShipmentRequest>
how can i add a prefix like "req:" as shown in the expected output as i am using jaxb to marshall and unmarshall

Creating xml using DOM in java

I am trying to create a an XML with multiple elements. Below is the code i am trying.
Element root = doc.createElement("root");
doc.appendChild(root);
Element member = doc.createElement("member");
root.appendChild(member);
Element name = doc.createElement("name");
name.appendChild(doc.createTextNode("xxx"));
member.appendChild(name);
Element phone = doc.createElement("phone");
phone.appendChild(doc.createTextNode("vvvv"));
member.appendChild(phone);
Element sss = doc.createElement("somethingNew");
root.appendChild(sss);
Element nnn = doc.createElement("name1");
nnn.appendChild(doc.createTextNode("AAA"));
sss.appendChild(nnn);
Element ppp = doc.createElement("phoneEx");
ppp.appendChild(doc.createTextNode("cc"));
sss.appendChild(ppp);
And the output i am getting is
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root>
<member>
<name>xxx</name>
<phone>vvvv</phone>
<name>xxx</name>
<phone>vvvv</phone>
</member>
<somethingNew/>
</root>
But i am expecting somthing like Below, Please help me where i am going wriong
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<root>
<member>
<name>xxx</name>
<phone>vvvv</phone>
<name>xxx</name>
<phone>vvvv</phone>
</member>
<somethingNew>
<name1>DDD</name1>
<phoneEx>CC</phoneEx>
</somethingNew>
</root>
Never Mind i could able to find the solution, I am appending to the first element all the time, That was the issue.Corrected my code

Java XSL Transformation complaining about binding prefix to namespace

I try to transform a XML-document which is a TCX-file from a Garmin Watch with XSL and the javax.xml.transform.Transformer. This is the document (shorten):
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<TrainingCenterDatabase xmlns="http://www.garmin.com/xmlschemas/TrainingCenterDatabase/v2">
<Activities>
<Activity Sport="Running">
<Id>2014-08-23T15:36:15.000Z</Id>
<Lap StartTime="2014-08-23T11:50:34.000Z">
<TotalTimeSeconds>3181.0</TotalTimeSeconds>
<DistanceMeters>7620.0</DistanceMeters>
<MaximumSpeed>21.887998580932617</MaximumSpeed>
<Calories>386</Calories>
<AverageHeartRateBpm>
<Value>119</Value>
</AverageHeartRateBpm>
<MaximumHeartRateBpm>
<Value>167</Value>
</MaximumHeartRateBpm>
<Intensity>Active</Intensity>
<TriggerMethod>Manual</TriggerMethod>
<Track>
<Trackpoint>
<Time>2014-08-23T11:50:34.375Z</Time>
<Position>
<LatitudeDegrees>48.17259333333333</LatitudeDegrees>
<LongitudeDegrees>10.191565</LongitudeDegrees>
</Position>
<AltitudeMeters>542.5505981445312</AltitudeMeters>
<DistanceMeters>0.0</DistanceMeters>
<HeartRateBpm>
<Value>70</Value>
</HeartRateBpm>
<SensorState>Absent</SensorState>
</Trackpoint>
[...]
</Track>
<Notes>Note</Notes>
</Lap>
<Notes>Note</Notes>
<Training VirtualPartner="false">
<Plan Type="Workout" IntervalWorkout="false">
<Name>Running</Name>
<Extensions/>
</Plan>
</Training>
<Creator xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Device_t">
<Name>Polar V800</Name>
<UnitId>0</UnitId>
<ProductID>13</ProductID>
</Creator>
</Activity>
</Activities>
<Author xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Application_t">
<Name>no application</Name>
</Author>
</TrainingCenterDatabase>
I use the javax.xml.transform.Transformer for this task and get a strange error. The transformer gives me the following error message:
Element "xmlns:xsi" cannot have "xmlns" as its prefix.
It belongs to the line:
<Creator xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="Device_t">
From my point of view, this line is valid. The given namespace is just bound to the prefix xsi. Is it necessary to configure the Transformer to detect it right or do I need to specify something in the xsl-file?
Thanks

Categories