Change tag name in XML - java

I have exception like this:
javax.xml.bind.UnmarshalException: unexpected element (uri:"", local:"tax"). Expected elements are <{}TaxGroup>
I have resposne which is String and look like this:
<?xml version="1.0" encoding="utf-8"?>
<tax xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<xml_message_type>tax</xml_message_type>
<version>
<xml_version>1.0</xml_version>
</version>
</tax>
How to iterate over this XML and repleace tag name tax with TaxGroup ?

It must mean that you try to unmarshal your XML which has a root element <tax> to Java Jaxb class which expects root element <TaxGroup> instead.
Definitelly XML does not match what unmarshaler was requested.
check what Java class do you expect to get out of this XML, and change it accordingly. Also check what element name defined in its JAXB annotation.

Related

When using Citrus <TestMessage> child tag of <payload> element I am getting the following error

cvc-complex-type.2.4.a: Invalid content was found starting with element
'TestMessage'. One of '{WC[##other:"http://www.citrusframework.org/schema/
testcase"]}' is expected.
Here is the screen shot XML dsl file click here to see xml image of citrus
This is because your <TestMessage> is not using any namespace. This is not allowed in payload element. You should use a proper XML namespace or go with <data> element instead of <payload>.

how to generate xml using single jaxb instance

I want to generate following xml structure using one jaxb marshaller instance.
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ABC>//root element
<BCD>
<DEF>
<EFG>xyz</EFG>
<FGH>
<GHI>PASSWORD_1</GHI>
<HIJ>false</HIJ>
</FGH>
</DEF>
</BCD>
<CDE/>
</ABC>
<ABC>//root element
<BCD>
<DEF>
<EFG>xyz</EFG>
<FGH>
<GHI>PASSWORD_2</GHI>
<HIJ>false</HIJ>
</FGH>
</DEF>
</BCD>
<CDE/>
</ABC>
<ABC>// root element
<BCD>
<DEF>
<EFG>xyz</EFG>
<FGH>
<GHI>PASSWORD_3</GHI>
<HIJ>false</HIJ>
</FGH>
</DEF>
</BCD>
<CDE/>
</ABC>
I above structure node ABC is root node which I want to repeat in same file for multiple records in list. Is there a way to do so in JaxB?
You cannot since this is not valid XML. XML documents must have exactly one root element.

How to create XML with declared namespace?

I am creating a xml request using java.
I am new in creating xmls using java.
Here is code:
Document doc = docBuilder.newDocument();
Element rootElement = doc.createElement("UserRequest");
rootElement.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:ns0", "https://com.user.req");
rootElement.setAttributeNS("http://www.w3.org/2000/xmlns/", "xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
doc.appendChild(rootElement);
// user element
Element user = doc.createElement("User");
rootElement.appendChild(user);
// userAttributes element
Element userAttr = doc.createElement("UserAttributes");
rootElement.appendChild(userAttr);
// name elements
Element name = doc.createElement("Name");
name.appendChild(doc.createTextNode("hello"));
userAttr.appendChild(name);
// value elements
Element value = doc.createElement("Value");
name.appendChild(doc.createTextNode("dude"));
userAttr.appendChild(value);
Expected output:
<?xml version="1.0" encoding="UTF-8"?>
<UserRequest
xmlns:ns0="https://com.user.req"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:type="ns0:UserRequest">
<User/>
<UserAttributes>
<Name>hello</Name>
<Value>dude</Value>
</UserAttributes>
</UserRequest>
Generated output:
<?xml version="1.0" encoding="UTF-8"?>
<UserRequest
xmlns:ns0="https://com.user.req"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
<User/>
<UserAttributes>
<Name>hello</Name>
<Value>dude</Value>
</UserAttributes>
</UserRequest>
How to get correct namespace (as shown at expected section).
There's nothing wrong with the namespaces in your generated output. However this is an accident ... you're using setAttributeNS() to do something it's not intended for.
Read up on XML namespace declarations and namespace prefixes. That will be a lot easier than trying to explain point-by-point why you're not getting what you expected. For example, xmlns is not a namespace prefix, and xsi:type is not a namespace.
Instead of trying to create the desired namespace declarations as if they were normal attributes, delete these two lines
rootElement.setAttributeNS("http://www.w3.org/2000/xmlns/",
"xmlns:ns0", "https://com.user.req");
rootElement.setAttributeNS("http://www.w3.org/2000/xmlns/",
"xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
and instead use
rootElement.setAttributeNS("http://www.w3.org/2001/XMLSchema-instance",
"xsi:type", "ns0:UserRequest");
This should give you most of your expected output, except for the ns0 namespace prefix declaration. It won't generate that because you're not using ns0 on any element or attribute. Did you mean to have
<ns0:UserRequest ...
in your expected output?

JDOM2 xpath finding nodes within a different namespace

I'm attempting to use JDOM2 in order to extract the information I care about out of a XML document. How do I get a tag within a tag?
I have been only partially successful. While I have been able to use xpath to extract <record> tags, the xpath query to extract the title, description and other data with in the record tags has been returning null.
I've been using Xpath successfully to extract <record> tags out of the document. To do this I use the follwing xpath query: "//oai:record" where the "oai" namespace is a namespace I made up in order to use xpath.
You can see the XML document I'm parsing here, and I've put a sample below: http://memory.loc.gov/cgi-bin/oai2_0?verb=ListRecords&set=cwp&metadataPrefix=oai_dc
<record>
<header>
<identifier>oai:lcoa1.loc.gov:loc.pnp/cph.3a02293</identifier>
<datestamp>2009-05-27T07:22:37Z</datestamp>
<setSpec>cwp</setSpec>
<setSpec>lcphotos</setSpec>
</header>
<metadata>
<oai_dc:dc xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/oai_dc/ http://www.openarchives.org/OAI/2.0/oai_dc.xsd">
<dc:title>Jubal A. Early</dc:title>
<dc:description>This record contains unverified, old data from caption card.</dc:description>
<dc:date>[between 1860 and 1880]</dc:date>
<dc:type>image</dc:type>
<dc:type>still image</dc:type>
<dc:identifier>http://hdl.loc.gov/loc.pnp/cph.3a02293</dc:identifier>
<dc:language>eng</dc:language>
<dc:rights>No known restrictions on publication.</dc:rights>
</oai_dc:dc>
</metadata>
</record>
If you look in the larger document you will see that there is never a "xmlns" attribute listed on any of the tags. There is also the matter of there being three different namespaces in the document ("none/oai", "oai_dc", "dc").
What is happening is that the xpath is matching nothing, and evaluateFirst(parent) is returning null.
Here is some of my code to extract the title, date, description etc. out of the record element.
XPathFactory xpf = XPathFactory.instance();
XPathExpression<Element> xpath = xpf.compile("//dc:title",
Filters.element(), null,
namespaceList.toArray(new Namespace[namespaceList.size()]));
Element tag = xpath.evaluateFirst(parent);
if(tag != null)
{
return Option.fromString(tag.getText());
}
return Option.none();
Any thoughts would be appreciated! Thanks.
In your XML, dc prefix mapped to the namespace uri http://purl.org/dc/elements/1.1/, so make sure you declared the namespace prefix mapping to be used in the XPath accordingly. This is part where the namespace prefix declare in your XML :
<oai_dc:dc
xmlns:oai_dc="http://www.openarchives.org/OAI/2.0/oai_dc/"
xmlns:dc="http://purl.org/dc/elements/1.1/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.openarchives.org/OAI/2.0/oai_dc/
http://www.openarchives.org/OAI/2.0/oai_dc.xsd">
XML parser only see the namespace explicitly declared in the XML, it won't try to open the namespace URL since namespace is not necessarily a URL. For example, the following URI which I found in this recent SO question is also acceptable for namespace : uuid:ebfd9-45-48-a9eb-42d

Update a single element in an xml document

Is it possible to parse and then modify a single element in an XML document?
I'm currently writing a script in ruby which needs to modify a value (specified by xpath) in an xml file. I'm currently using the REXML library to do this:
xmldocument = Document.new(File.new(filename))
property = XPath.first(xmldocument, "/parent/element/property")
property.text = "New property value"
puts xmldocument
Where the input xml is:
<?xml version="1.0" encoding="UTF-8"?>
<parent>
<element>
<property>Old property value</property>
<verbose />
</element>
...
(more elements here)
...
</parent>
And the output is:
<?xml version='1.0' encoding='UTF-8'?>
<parent>
<element>
<property>New property value</property>
<verbose/>
</element>
...
(more elements here)
...
</parent>
You should notice that the output xml is slightly reformatted and more than my desired change are made. For example the tag <verbose /> is changed to <verbose/> and double quotes are replaced with single quotes in the first line.
What is the best way to modify just a given element of an xml file and leave the rest of the file intact? Ideally, there is a solution for Ruby but I'd love to know the solution in other languages such as Java.
In Java, the Saxon library should accomplish everything you're looking for:
http://sourceforge.net/projects/saxon/

Categories