How can I validate a XML file? - java

my contacts.xml file is:
<?xml version="1.1" encoding="UTF-8" standalone="no" ?>
<Directory>
<Contacts DeviceID="" FolderID="" FolderName="">
<Contact contacttype="0" id="111" optype="0">
<FirstName>shiva1</FirstName>
<wsuniqueid>00000000A4DACC2711A8D24C9AC2C2999311125BC4306A00</wsuniqueid>
</Contact>
<Contact contacttype="0" id="222" optype="0">
<FirstName>shiva2</FirstName>
<wsuniqueid>00000000A4DACC2711A8D24C9AC2C2999311125BC4306A01</wsuniqueid>
</Contact>
</Contacts>
</Directory>
when i want to insert some node or data by query:
insert node <a/> into doc('contacts.xml')//Directory/Contacts/Contact[#id = '111']
from linux command it is giving error XQDY0084 ..

The error code is not related to the insertion (directly). It indicated you are using strict validation somewhere, and you are not adhering to the strict validation rules. Perhaps because the system cannot find an appropriate schema file in which Directory is a valid root element.
Namespace your elements, and use an appropriate XML Schema, or decide not to validate at all.
HTH!

Related

Java signing XML File - prevent standard XML signature namespace inside signature

I'm signing an XML File where I'm using the Java XML Digital Signature API, available from Java 6 up to now.
Web-Source: http://www.oracle.com/technetwork/articles/javase/dig-signature-api-140772.html
The Signature looks like following:
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">[...]</Signature>
Now I want to know, is there any way, to sign the XML File, to prevent the API to determine this xmlns="http://www.w3.org/2000/09/xmldsig#"inside my tag, so that I just have following:
<Signature>...</Signature>
I'm very thankful for any clues.
Thank you #Vadim for your answer. Lets give more details for my problem. I got a XML Structure like:
<?xml version="1.0" encoding="UTF-8" ?>
<tests xmlns="schema1">
<test>
</test>
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
</Signature>
</tests>
How can I get this working? Because in a third party system I Need to check it against a Schema, where I define the structure of the signature by myself, so there should be like two xmlns inside my
Per XML standard you have to have namespace defined for <Signature> element, so it can be as you have it or outside on parent element with prefix. As
<rootElemnt xmlns:sig="http://....">
<sig:Signature>....
But, why it bother you? Without it <Signature> tag belongs to default namespace of parent element and not to proper Signature namespace.
UPDATED If you have two namespaces you have to have two xmlns declarations. One can be default second must have prefix. or both must have prefixes.
If your custom elements are in xmlns="schema1", I think you need to look either how to make <sig:Signature xmlns:sig="http://www.w3.org/2000/09/xmldsig#"> or
<sch1:tests xmlns:sch1="schema1">
<sch1:test>
</sch1:test>
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
...
<sch1:customElement>...</sch1:customElement>
</Signature>
</sch1:tests>
It depends on how you build full XML. Sorry, I'do not know how to do that in Java XML Digital Signature API (never used it directly, just through WSDL policy), but all other tools have an ability to handle namespace prefixes
also it can look like:
<tests xmlns="schema1">
<test>
</test>
<Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
...
<sch1:customElement xmlns:sch1="schema1">...</sch1:customElement>
</Signature>
</tests>
Perhaps it should look like that by defualt, but if not I guess if you marshal customElement into XML separately and then add it into Signature it must be like that.

Selective XML parsing

This the xml file I have
<?xml version="1.0" encoding="UTF-8"?>
<Bank>
<Account type="saving">
<Id>1001</Id>
<Name>Jack Robinson</Name>
<Amt>10000</Amt>
</Account>
<Account type="current">
<Id>1002</Id>
<Name>Sony Corporation</Name>
<Amt>1000000</Amt>
</Account>
</Bank>
I need to parse this xml and get the contents between <Bank>...</Bank>. My output xml should be
<Account type="saving">
<Id>1001</Id>
<Name>Jack Robinson</Name>
<Amt>10000</Amt>
</Account>
<Account type="current">
<Id>1002</Id>
<Name>Sony Corporation</Name>
<Amt>1000000</Amt>
</Account>
Any ideas on how to achieve this using Java?
First of all:
your output XML is not valid XML.
XML must have root element which you try to remove.
As #Seelenvirtuose said, there are tons of ways to do what you want on many levels.
From simple manipulating original XML as String and up to using DOM model, JAXB, XPath/XQuery, or XSLT. It is matter of your choice.
As example with Apache commons utils:
String resultString = org.apache.commons.lang.StringUtils.substringBetween(originalXMLString,"<Bank>","</Bank>").trim();
Of course your output can be only String, because it is not valid XML. Then you can do with that String whatever you want - print it, store in file or DB etc...

Is it possible to create an object from XML file using DOM?

I use DOM parser to read data from XML file. I know how to read, modify and write back the data. However, I would like to know if it is possible to create an object from an XML file.
I have an XML file which looks like this:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE people SYSTEM "validator.dtd">
<people>
<student>
<name>John</name>
<course>Computer Technology</course>
<semester>6</semester>
<scheme>E</scheme>
</student>
<student>
<name>Foo</name>
<course>Industrial Electronics</course>
<semester>6</semester>
<scheme>E</scheme>
</student>
</people>
and I would like to make it an objects out of it so I can pass them around. Does a solution exist ?
Yes. This is possible through JAXB (Java API for XML binding)
All JAXB implementations provide a tool called a binding compiler to bind a XML schema in order to generate the corresponding Java classes.
For details refer: http://www.oracle.com/technetwork/articles/javase/index-140168.html#xmp1
You could have a look at XML beans or JAXB libraries. In case you don't have a schema file but have a sample XML file, you could create one using inst2xsd tool of xmlbeans. http://xmlbeans.apache.org/docs/2.0.0/guide/tools.html. This could get you started with the schema.

How to Add Namespace to XML in java?

I have below xml. Namespace is missing in the below xml.
<?xml version="1.0" encoding="UTF-8"?>
<policy>
<num-drivers>123</num-drivers>
<risk-policy-ind>false</risk-policy-ind>
<premium-amt>23.00</premium-amt>
</policy>
Looking for java code to take the above xml as input and add namespace (xmlns) element to it? The expected output xml is as below:
<?xml version="1.0" encoding="UTF-8"?>
<policy xmlns="http://aaa.bbb.com">
<num-drivers>123</num-drivers>
<risk-policy-ind>false</risk-policy-ind>
<premium-amt>23.00</premium-amt>
</policy>
First of all, in the above xml, risk-policy-ind tag is not properly closed. In XML all tags are custom tags, they should close. Moreover,in xml, tags are performed without namespace.
If u just want to add xmlns attribute to policy tag, create policy element using w3c.dom.Element and set attribute using setAttribute function

Preserving the CDATA format with a SAX parser

I'm trying to parse an XML file and insert some attributes in my database. I'm developing in JAVA and using SAX to parse the XML file.
My problem is that when I read an attribute in CDATA format I only get what the CDATA contains. Perhaps I wan't to keep the CDATA format?
For example with the XML below :
<?xml version="1.0" encoding="UTF-8"?>
<Bank>
<Account type="saving">
<Id>1001</Id>
<Name><![CDATA[<Jack> <Robinson>]]></Name>
<Amt>10000</Amt>
</Account>
<Account type="current">
<Id>1002</Id>
<Name>Sony Corporation</Name>
<Amt>1000000</Amt>
</Account>
</Bank>
I would like to get the Name and have it like this <![CDATA[<Jack> <Robinson>]]> and not only <Jack> <Robinson> which is what I am getting.
Can anyone help me with this issue please.
PS : Sorry for my English, I'm french.
Best regards,
Like #Quentin asked, I am curious why do you care about markup.
Did you consider appending <![CDATA[ and ]] using StringBuffer manually in your output.

Categories

Resources