Failing to extract xml value using Jdom & Xpath
I have a method (getSingleNodeValue()) which when passed an xpatch expression will extract the value of the specified element in the xml document refered to in 'doc'. Assume doc at this point has been initialised as shown below and xmlInput is the buffer containing the xml content.
SAXBuilder builder = null;
Document doc = null;
XPath xpathInstance = null;
doc = builder.build(new StringReader(xmlInput));When i call the method, i pass the following xpath xpression
/TOP4A/PERLODSUMDEC/TINPLD1/text()Here is the method. It basically just takes an xml buffer and uses xpath to extract the value:
public static String getSingleNodeValue(String xpathExpr) throws Exception{
Text list = null;
try {
xpathInstance = XPath.newInstance(xpathExpr);
list = (Text) xpathInstance.selectSingleNode(doc);
} catch (JDOMException e) {
throw new Exception(e);
}catch (Exception e){
throw new Exception(e);
return list==null ? "?" : list.getText();
}The above method always returns "?" i.e. nothing is found so 'list' is null.
The xml document it looks at is
<TOP4A xmlns="http://www.testurl.co.uk/enment/gqr/3232/1">
<HEAD>
<Doc>ABCDUK1234</Doc>
</HEAD>
<PERLODSUMDEC>
<TINPLD1>10109000000000000</TINPLD1>
</PERLODSUMDEC>
</TOP4A>The same method works with other xml documents so i am not sure what is special about this one. There is no exception so the xml is valid xml. Its just that the method always sets 'list' to null. Any ideas?
Edit
Here is a running program testing the above:
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.xpath.*;
import java.io.IOException;
import java.io.StringReader;
public class XpathTest {
public static String getSingleNodeValue(String xpathExpr, String xmlInput) throws Exception{
Text list = null;
SAXBuilder builder = null;
Document doc = null;
XPath xpathInstance = null;
try {
builder = new SAXBuilder();
doc = builder.build(new StringReader(xmlInput));
xpathInstance = XPath.newInstance(xpathExpr);
list = (Text) xpathInstance.selectSingleNode(doc);
} catch (JDOMException e) {
throw new Exception(e);
}catch (Exception e){
throw new Exception(e);
return list==null ? "Nothing Found" : list.getText();
public static void main(String[] args){
String xmlInput1 = "<TOP4A xmlns=\"http://www.testurl.co.uk/enment/gqr/3232/1\"><HEAD><Doc>ABCDUK1234</Doc></HEAD><PERLODSUMDEC><TINPLD1>10109000000000000</TINPLD1></PERLODSUMDEC></TOP4A>";
String xpathExpr = "/TOP4A/PERLODSUMDEC/TINPLD1/text()";
XpathTest xp = new XpathTest();
try {
System.out.println(xp.getSingleNodeValue(xpathExpr, xmlInput1));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}When i run the above, the output is
Nothing foundEdit
I have run some further testing and it appears that if i remove the namespace url it does work. Not sure why yet. Is there any way i can tell it to ignore the namespace?
Edited by: ziggy on Sep 3, 2011 4:57 PM
ziggy wrote:
<TOP4A xmlns="http://www.testurl.co.uk/enment/gqr/3232/1">
<HEAD>
<Doc>ABCDUK1234</Doc>
</HEAD>
<PERLODSUMDEC>
<TINPLD1>10109000000000000</TINPLD1>
</PERLODSUMDEC>
</TOP4A>
It works fine, the problem is not with namespace, it is with url have given.
Editing:
Found a way to say the program to ignore namespace.
You have to use Xpath.addNamespace(prefix,uri), and pass the prefix to your pattern string.
If not clear refer the below code:
import org.jdom.*;
import org.jdom.input.*;
import org.jdom.xpath.*;
import java.io.IOException;
import java.io.StringReader;
public class XpathTest {
public static String getSingleNodeValue(String xpathExpr, String xmlInput) throws Exception{
Text list = null;
SAXBuilder builder = null;
Document doc = null;
XPath xpathInstance = null;
try {
builder = new SAXBuilder();
doc = builder.build(new StringReader(xmlInput));
xpathInstance = XPath.newInstance(xpathExpr);
xpathInstance.addNamespace("ns","http://www.testurl.co.uk/enment/gqr/3232/1");
list = (Text) xpathInstance.selectSingleNode(doc);
} catch (JDOMException e) {
throw new Exception(e);
}catch (Exception e){
throw new Exception(e);
return list==null ? "Nothing Found" : list.getText();
public static void main(String[] args){
String xmlInput1 = "<TOP4A xmlns=\"http://www.testurl.co.uk/enment/gqr/3232/1\"><HEAD><Doc>ABCDUK1234</Doc></HEAD><PERLODSUMDEC><TINPLD1>10109000000000000</TINPLD1></PERLODSUMDEC></TOP4A>";
String xpathExpr = "/ns:TOP4A/ns:PERLODSUMDEC/ns:TINPLD1/text()";
XpathTest xp = new XpathTest();
try {
System.out.println(xp.getSingleNodeValue(xpathExpr, xmlInput1));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}Edited by: 833545 on Sep 8, 2011 11:35 PM
Similar Messages
-
Best way to extract XML value wiith an xpath
Hello,
I wonder what is the best way to extract text value from XmlType with an xpath.
I need to insert a row inside a table where the row's data come from xpath extractions of an XmlType. I do a lot of (approximative 20) :
EXTRACTVALUE(var.myxmltype , '/an/xpath/to/extract/elem1').
EXTRACTVALUE(var.myxmltype , '/an/xpath/to/extract/elemI').
EXTRACTVALUE(var.myxmltype , '/an/xpath/to/extract/elem20').
inside the insert statement
Is this way is the best or is there a more optimal way ?
For example extracting the node '/an/xpath/to/extract/' and sarting from this node extracting "elem1", ... , "elemI", "elemN" children.
Thanks for your help,
Regards,
NicolasHi Nicolas,
The answer depends on your actual storage method (binary, OR, CLOB?), and db version.
You can try XMLTable, it might be better in this case :
SELECT x.elem1, x.elem2, ... , x.elem20
FROM your_table t
, XMLTable(
'/an/xpath/to/extract'
passing t.myxmltype
columns elem1 varchar2(30) path 'elem1'
, elem2 varchar2(30) path 'elem2'
, elem20 varchar2(30) path 'elem20'
) x
; -
Extract Node Values using XPATH
This may be a repeat question.
No offence meant
I am using XPATH to extract values from the nodes.
I need to extract the value of i:RequestedBy Node using XPATH
<?xml version="1.0" encoding="UTF-8"?>
<i:Interest>
<i:Status>0</i:Status>
<i:Generation>2</i:Generation>
<i:Details xsi:type="i:vanilla.details.stock">
<i:RequestedBy>AA.MM</i:RequestedBy>
The above XML is of type Document
Document msgDoc;
XPath xpath = XPathFactory.newInstance().newXPath();
XPathExpression expr;
Object result=null;
try {
expr = xpath.compile("i:Interest/i:Details/i:RequestedBy/text()");
result = expr.evaluate(msgDoc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
System.out.println(nodes.item(i).getNodeValue());
} catch (XPathExpressionException e1) {
e1.printStackTrace();
I am getting null/
Is my XPATH correct?I don't know if your XPath is correct or not. Your XML is not well-formed because the namespace declarations are missing. And I don't see where you set the namespace context for your XPath object; that is quite likely why it doesn't do what you want.
-
I need for a simple example of reading a xml file using jdom
Hello
I have been looking for a simple example that uses Jdom to read am xml file and use the information for anything( ), and I just can't find one.since I'm just beggining to understand how things work, I need a good example.thanks
here is just a simple cod for example:
<xmlMy>
<table>
<item name="first" value="123" createdDate="1/1/90"/>
<item name="second" value="456" createdDate="1/4/96"/>
</table>
<server>
<property name="port" value="12345"/>
<property name="maxClients" value="3"/>
</server>
</xmlMy>DaveHi,
FileInputStream fileInputStream = null;
try {
fileInputStream = new FileInputStream("my_xml_file.xml");
} catch (FileNotFoundException e) {
e.printStackTrace();
} finally{
if (fileInputStream == null) return;
SAXBuilder saxBuilder = new SAXBuilder();
saxBuilder.setEntityResolver(new EntityResolver() {
public InputSource resolveEntity(String publicId, String systemId) throws SAXException, IOException {
return new InputSource(new StringReader(""));
Document document = null;
try {
document = saxBuilder.build(fileInputStream);
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally {
if (document == null) return;
Element element = document.getRootElement();
System.out.println(element.getName());
System.out.println(element.getChild("table").getName()); -
Parsing a XML file using Jdom-Problem.
Hi all,
I am reposting it as I was told to format the code and send it again.
I am trying to parse a xml file using a jdom java code.This code works fine if I remove xmlns attribute in the root element. (I get the expected result) .If the "xmlns" attribute is put in the xml as it should be then the parsing and retrieving returns null. Please tell me how to fix this issue.
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Xml
<process name="CreateKBEntryService" targetNamespace="http://serena.com/CreateKBEntryService" suppressJoinFailure="yes" xmlns:tns="http://serena.com/CreateKBEntryService" xmlns="http://schemas.xmlsoap.org/ws/2003/03/business-process/" xmlns:bpelx="http://schemas.oracle.com/bpel/extension" xmlns:ora="http://schemas.oracle.com/xpath/extension" xmlns:nsxml0="http://localhost:8080/axis/services/CreateKBEntryService" xmlns:nsxml1="http://DefaultNamespace" xmlns:bpws="http://schemas.xmlsoap.org/ws/2003/03/business-process/">
<partnerLinks>
<partnerLink name="client" partnerLinkType="tns:CreateKBEntryService" myRole="CreateKBEntryServiceProvider"/>
<partnerLink name="CreateKBEntryPartnerLink" partnerLinkType="nsxml0:CreateKBEntryLink" partnerRole="CreateKBEntryProvider"/>
</partnerLinks>
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~`
Java:
import java.io.*;
import java.util.*;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
public class sample1 {
public static void main(String[] args) throws Exception {
// create a XML parser and read the XML file
SAXBuilder oBuilder = new SAXBuilder();
Document oDoc = oBuilder.build(new File("**xml file location**"));
Element root = oDoc.getRootElement();
System.out.println(root.getName());
String tgtns= root.getAttributeValue("targetNamespace");
System.out.println("tgt ns "+ tgtns);
List list= root.getChildren("partnerLinks");
Iterator it1= list.iterator();
System.out.println("Iterator 1 - "+list.size());
while(it1.hasNext()){
Element partnerlinks = (Element)it1.next();
List list2= partnerlinks.getChildren("partnerLink");
System.out.println("iterator 2 - "+list2.size());
}~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Result:
Without Xmlns in xml file(Expected and correct output)
process
tgt ns http://serena.com/CreateKBEntryService
Iterator 1 - 1//expected and correct result that comes when I remove xmlns attribute from xml
iterator 2 - 2
Result with xmlns:
process
tgt ns http://serena.com/CreateKBEntryService
Iterator 1 - 0 //instead of 0 should return 1LOL
This is what you get for working 12 hours straight....
I changed:
xmlObject["mydoc"]["modelglue"]["event-handlers"]["event-handler"][i].xmlAttrib utes["name"]<br>
to:
#mydoc["modelglue"]["event-handlers"]["event-handler"][i].xmlAttrib utes["name"]#<br>
xmlObject is the name of my xml object in memory, and then you reference from the root of the xml doc down the chain.
Sorry for the inconvenience,
Rich -
XML Signature using an XPath filter
I want to sign an XML document, just based on the content of a particular element in the document. Based on the jwsdp docs it looked pretty simple, but I'm getting strange results. I'll get the same digest value in the SignedInfo for different Xpath filter strings.
Anybody out there have good luck trying to sign XML documents using Xpath filters?
I'm using JDK 1.5.05 and JWSDP 1.6.
Here is my simple XML doc
<?xml version="1.0" encoding="UTF-8"?>
<myns:whole_doc xmlns:myns="http://namespace.myns.com/test"
attr1="attr_one" attr2="attr_two" attr3="attr_three">
<myns:part_one attr="attr_one">
<myns:tag1>this is part_one tag_one</myns:tag1>
<myns:tag2>this is part_one tag_two</myns:tag2>
</myns:part_one>
<myns:part_two attr="attr_two">
<myns:tag1>this is part_two tag_one</myns:tag1>
<myns:tag2>this is part_two tag_two</myns:tag2>
</myns:part_two>
<myns:part_three attr="attr_three">
<myns:tag1>this is part_three tag_one</myns:tag1>
<myns:tag2>this is part_three tag_two</myns:tag2>
</myns:part_three>
</myns:whole_doc>
When I use an XPath filter = "/myns:whole_doc/myns:part_one" I get the same digest as when I use "/myns:whole_doc/myns:part_two", which is the same digest when I don't use XPath filtering. See example output below
<?xml version="1.0" encoding="UTF-8" ?>
- <myns:whole_doc xmlns:myns="http://namespace.myns.com/test" attr1="attr_one" attr2="attr_two" attr3="attr_three">
- <myns:part_one attr="attr_one">
</myns:part_three>
- <Signature xmlns="http://www.w3.org/2000/09/xmldsig#">
- <SignedInfo>
<CanonicalizationMethod Algorithm="http://www.w3.org/TR/2001/REC-xml-c14n-20010315#WithComments" />
<SignatureMethod Algorithm="http://www.w3.org/2000/09/xmldsig#dsa-sha1" />
- <Reference URI="">
- <Transforms>
<Transform Algorithm="http://www.w3.org/2000/09/xmldsig#enveloped-signature" />
- <Transform Algorithm="http://www.w3.org/TR/1999/REC-xpath-19991116">
<XPath>/myns:whole_doc/myns:part_two</XPath>
</Transform>
</Transforms>
<DigestMethod Algorithm="http://www.w3.org/2000/09/xmldsig#sha1" />
<DigestValue>9D5CFDkWd9bHx65txuHOeXWeTns=</DigestValue>
</Reference>
</SignedInfo>
<SignatureValue>D686H//H5A0zDrlQx8+0fBNpVeJGgWdTXivlI8S/+WqB/E4oBYzeIQ==</SignatureValue>
</Signature>
</myns:whole_doc>It took me all day, but I did find the answer.
When using XPath filters in references, you can't use XPath exactly the way you would use XPath to find nodes in a document. I can't exactly explain why, but it has to do with the fact that the XPath statement is acting as a filter.
Anyway, to just include the part_one element from the following abbreviated XML
<myns:whole_doc> .....
<myns:part_one> ..... </myns:part_one>
<myns:part_two> .... </myns:part_two>
</myns:whole_doc>
the XPath statement should be "ancestor-or-self::myns:part_one"
to make sure that part_one was part of whole_doc you could use something like
(ancestor-or-self::node() = /myns:whole_doc/myns:part_one)
This allows me to sign a document, but still allow portions outside of whole_doc/part_one to change. -
How to get the "encoding" of a XML file using JDOM
As in XML file, <?xml version="1.0" encoding="UTF-8" ?> indicates the encoding of this file
while using JDOM to parse a XML file, how can I get the encoding type?
thanx!!!What my program do is to get the encoding of XML files and convert them to UTF-8 encoding files, while I need this "encoding" information of the original XML document thus I can convert...
After reading specifications and JDOM docs, the truth turns to be disappointed, no function is provided to get this information in JDOM level 2(the current released one), while it's promissed that this function will be provided in JDOM level API....
Thanx all for your help and attention!!! -
How to get XML value using servlet?
how to get values from a XML file using servlet ? Thank you.
for example: how can I get the location value (aaa) from this XML file?
abc.xml:
<business>
<location>aaa</location>
</business>Try to use XML Parsers to do the job.
Use DOM or SAX Parsers that are freely available.
Some of the popular ones are xerces, DOM, SAX. -
Creating XML Doc using JDOM... HELP!!!!!
I've got a constructor which creates an Annotation object ... each object has 4 fields;
Annotation Owner
Date/time submitted
ID
Annotation Text
When each Annotation object is constructed it is placed on a Vector array.
I now want to create an XML Document for the Annotation object on the Vector Array...
I am using JDOM and have managed to create a simple constructor:
// Class XMLDocument...
// This class is a subclass of MooNotes 2 that is designed to construct a XMLDocument containing a particular
// Annotations details...
class XMLDocument {
long docID;
org.jdom.Document xmldoc;
* <p>
* Build a simple XML document in memory
* </p>
// Constructor for the XMLDocument class...
// This constructor will construct an
// XMLDocument and populate the data fields with the particular
// Annotation contents.
// The constructor is passed a DOCUMENT ID and a JDOM document when invoked and it uses this data to construct
// the XML Document.
public XMLDocument(long docID, org.jdom.Document xmldoc) {
this.docID = docID;
this.xmldoc = xmldoc;
xmldoc.setDocType(new org.jdom.DocType("html", "-//W3C//DTD XHTML 1.0 Transitional//EN", "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"));
org.jdom.DocType docType = xmldoc.getDocType();
messagearea.append("XMLDocument Element: " + docType.getElementName() + " \n");
messagearea.append("XMLDocument Public ID: " + docType.getPublicID() + " \n");
messagearea.append("XMLDocument SystemID: " + docType.getSystemID() + " \n");
messagearea.append("Document successfully built " + " \n");
How do I construct a Document to accept and hold the contents of an Annotation Object?
P.S.
AnnotationSubmitter is a String
So is Date/time
ID is a long number
Annotation Text is also a StringSince your constructor receives a org.jdom.Document already, it's difficult to figure out what you're trying to do. I'm also not sure what you mean by a Vector array, but I'm gonna give it a shot. To construct a simple org.jdom.Document (without namespace) I would do the following:
Element root=new Element("ALL_ANNOTATIONS");
Document=new Document(root);
doc.setDocType(new DocType( ...[your stuff]... ));
and loop through your annotations objects
for(int i=0;i<n;++i)
root.addChild(new Element("ANNOTATION")
.addChild(new Element("ANNOTATION_OWNER")
.setText(annotObjects.getAnnotationOwner()))
.addChild(new Element("ANNOTATION_TIME")
.setText(......getTime()))
.addChild(new Element("ANNOTATION_ID")
.setText(""+annotObjects[i].getID()))
.addChild(new Element("ANNOTATION_TEXT")
.setText(......getText()));
In the end this should give you the equivalent of:
<ALL_ANNOTATIONS>
<ANNOTATION>
<ANNOTATION_OWNER>
Text of First Annotation Owner
</ANNOTATION_OWNER>
<ANNOTATION_TIME>
Time/Date of annotation
</ANNOTATION_TIME>
<ANNOTATION_ID>
1234567
</ANNOTATION_ID>
<ANNOTATION_TEXT>
Some Annotation Text
</ANNOTATION_TEXT>
</ANNOTATION>
<ANNOTATION>
etc. N-1 Times.
</ANNOTATION>
</ALL_ANNOTATIONS>
Hope that helps. -
How to read XML Values using JAVA
Hi FRNDS,
I uploaded one XML File in imported Archieves, i have to read the value form XML uisng JAVA code,
using XSL able to read like xsl variable name='TimeZone' select =$linkDoc//...........*
But using java i have to read the value from XML,is it possible to read the value??
please give me some sample code.
regards,
Raja SekharBut using java i have to read the value from XML,is it possible to read the value??
Check the below JAVA code from help:
http://help.sap.com/saphelp_nwpi71/helpdata/en/55/7ef3003fc411d6b1f700508b5d5211/content.htm
you can ignore the XSLT part and configure only the JAVA code...check if this suits your needs.
Regards,
Abhishek. -
Extracting XML values with namespace prefixes
Hint: I put this topic in "SQL and PL/SQL" but that was a bad idea, I think.
Hi!
I have a XML file:<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="http://test.epuap.gov.pl/FeResourceServlet/wzor_lokalny/InstytucjaPubliczna/Deklaracja_DT/styl.xsl"?>
<wnio:Deklaracja
xmlns:adr="http://crd.gov.pl/xml/schematy/adres/2008/05/09/"
xmlns:ds="http://www.w3.org/2000/09/xmldsig#"
xmlns:ev="http://www.w3.org/2001/xml-events"
xmlns:inst="http://crd.gov.pl/xml/schematy/instytucja/2008/05/09/"
xmlns:meta="http://crd.gov.pl/xml/schematy/meta/2008/05/09/"
xmlns:oso="http://crd.gov.pl/xml/schematy/osoba/2008/05/09/"
xmlns:str="http://crd.gov.pl/xml/schematy/struktura/2009/11/16/"
xmlns:wnio="http://test.epuap.gov.pl/FeResourceServlet/wzor_lokalny/InstytucjaPubliczna/Deklaracja_DT/"
xmlns:xforms="http://www.w3.org/2002/xforms"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xxforms="http://orbeon.org/oxf/xml/xforms"
xsi:schemaLocation="http://test.epuap.gov.pl/FeResourceServlet/wzor_lokalny/InstytucjaPubliczna/Deklaracja_DT/ http://test.epuap.gov.pl/FeResourceServlet/wzor_lokalny/InstytucjaPubliczna/Deklaracja_DT/schemat.xsd">
<wnio:OpisDokumentu><str:CID>[email protected]</str:CID><meta:Identyfikator typIdentyfikatora="idFormularza"><meta:Wartosc>InstytucjaPubliczna/Deklaracja podatku od środków transportowych DT-1</meta:Wartosc></meta:Identyfikator></wnio:OpisDokumentu>
<wnio:Naglowek>
<wnio:KodFormularza kodSystemowy="DT-1(3)"/>
<wnio:WariantFormularza>3</wnio:WariantFormularza>
<wnio:Rok>2008</wnio:Rok>
<wnio:ObowiazekSkladaniaDeklaracji>1</wnio:ObowiazekSkladaniaDeklaracji>
<wnio:MiejsceSkladania/>
</wnio:Naglowek>
<wnio:Podatnik>
<wnio:OsobaPrawna>
<wnio:NIP>345678901</wnio:NIP>
<wnio:PelnaNazwa>Testowa Instytucja Publiczna</wnio:PelnaNazwa>
<wnio:NazwaSkrocona/>
<wnio:REGON>234512345</wnio:REGON>
</wnio:OsobaPrawna>
<wnio:AdresSiedzibyZamieszkania>
<wnio:KodKraju>PL</wnio:KodKraju>
<wnio:Wojewodztwo>ŚLĄSKIE</wnio:Wojewodztwo>
<wnio:Powiat>Katowice</wnio:Powiat>
<wnio:Gmina>Katowice (gmina miejska)</wnio:Gmina>
<wnio:Ulica>ul. Mikołowska</wnio:Ulica>
<wnio:NrDomu>100</wnio:NrDomu>
<wnio:NrLokalu>924</wnio:NrLokalu>
<wnio:Miejscowosc>Katowice (miasto)</wnio:Miejscowosc>
<wnio:KodPocztowy>40-065</wnio:KodPocztowy>
<wnio:Poczta>Katowice</wnio:Poczta>
</wnio:AdresSiedzibyZamieszkania>
</wnio:Podatnik>
<wnio:PozycjeSzczegolowe>
<wnio:D.1>
<wnio:P_22/>
<wnio:P_23/>
</wnio:D.1>
<wnio:D.2>
<wnio:P_28/>
<wnio:P_29/>
</wnio:D.2>
<wnio:P_82/>
<wnio:P_83/>
</wnio:PozycjeSzczegolowe>
<wnio:Zalacznik_DT-1A>
<wnio:P_1/>
<wnio:P_2/>
<wnio:P_22>1</wnio:P_22>
</wnio:Zalacznik_DT-1A>
<wnio:Zalacznik_DT-1A>
<wnio:P_1/>
<wnio:P_2/>
<wnio:P_22>3</wnio:P_22>
</wnio:Zalacznik_DT-1A>
</wnio:Deklaracja>and an overloaded function:
function dodaj_DT1_z_xml(p_dt1 in out nocopy XMLType) return number as
r_dt1 POD_SRTR_DT1%rowtype;
r_dt1a POD_SRTR_DT1A%rowtype;
r_sdek POD_SRTR_DEKLARACJE%rowtype;
l_wariantDT1 number(2);
l_xosf XMLType;
l_xosp XMLType;
l_xadr XMLType;
l_xszcz XMLType;
l_zal XMLType;
l_z XMLType;
l_zno pls_integer;
l_xml XMLType;
l_ns varchar2(10);
-- xmlns:wnio="http://test.epuap.gov.pl/FeResourceServlet/wzor_lokalny/InstytucjaPubliczna/Deklaracja_DT/"
begin
if --p_dt1.existsNode('/wnio:Deklaracja')=0 and
p_dt1.existsNode('/Deklaracja','xmlns:wnio="http://test.epuap.gov.pl/FeResourceServlet/wzor_lokalny/InstytucjaPubliczna/Deklaracja_DT/"')=0
then return -2; end if;
if p_dt1.existsNode('/wnio:Deklaracja')=1 then
l_ns := 'wnio:';
end if;
if p_dt1.existsNode('/'||l_ns||'Deklaracja')=0 then return -2; end if;
if p_dt1.existsNode('/'||l_ns||'Deklaracja/'||l_ns||'Naglowek')=0 then return -3; end if;
if p_dt1.existsNode('/'||l_ns||'Deklaracja/'||l_ns||'Podatnik')=0 then return -4; end if;
if p_dt1.existsNode('/'||l_ns||'Deklaracja/'||l_ns||'PozycjeSzczegolowe')=0 then return -5; end if;
return 1;
end;
function dodaj_DT1_z_xml(p_dt1 in out nocopy CLOB) return number is
l_xml XMLType;
begin
l_xml := XMLType(p_dt1);
return dodaj_DT1_z_xml(l_xml);
end dodaj_DT1_z_xml;I run the code:declare
v_clob CLOB;
v_out number;
begin
SELECT dok_content INTO v_clob FROM EPUAP.epuap_dokumenty WHERE dok_id=13;
v_out := POD_PCK_SRTR_DT1.DODAJ_DT1_Z_XML(v_clob);
DBMS_OUTPUT.put_line(v_out);
end;the output is -2. What must I change to find the node "wnio:Deklaracja"? I'm newbie in XML and namespaces :(
Help me, please...
Oracle version is 10.2.0.3Hi,
If you declare a namespace prefix then corresponding elements must be qualified.
So, either do :
p_dt1.existsNode('/wnio:Deklaracja','xmlns:wnio="http://test.epuap.gov.pl/FeResourceServlet/wzor_lokalny/InstytucjaPubliczna/Deklaracja_DT/"')or use a default namespace declaration :
p_dt1.existsNode('/Deklaracja','xmlns="http://test.epuap.gov.pl/FeResourceServlet/wzor_lokalny/InstytucjaPubliczna/Deklaracja_DT/"')BTW, could you explain what are you trying to accomplish? -
How to modify an XML values using XSLT and Java
Hi Friends,
I wish to modify a simple xml to another xml using XSLT, please let me know what are the things i need to know to convert the XML to another XML.
If somebody can give a sample code, it will be great...
I wish to do the following sample conversion of XML
<?xml version="1.0" encoding="ISO-8859-1" ?>
<ROOT>
<NAME>TEST1</NAME>
</ROOT>so that the results look like below....
<?xml version="1.0" encoding="ISO-8859-1" ?>
<ROOT>
<NAME>TEST2</NAME>
</ROOT>Notice TEST1 is replaced with TEST2 in the 2nd XML, I wish to get that result...please help.
Thanks and Regards,
JGThanks for your nice reply... do you have a sample code...
-
Extracting XML values with namespace
Hi!
I have a XML file:<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="http://test.epuap.gov.pl/FeResourceServlet/wzor_lokalny/InstytucjaPubliczna/Deklaracja_DT/styl.xsl"?>
<wnio:Deklaracja
xmlns:adr="http://crd.gov.pl/xml/schematy/adres/2008/05/09/"
xmlns:ds="http://www.w3.org/2000/09/xmldsig#"
xmlns:ev="http://www.w3.org/2001/xml-events"
xmlns:inst="http://crd.gov.pl/xml/schematy/instytucja/2008/05/09/"
xmlns:meta="http://crd.gov.pl/xml/schematy/meta/2008/05/09/"
xmlns:oso="http://crd.gov.pl/xml/schematy/osoba/2008/05/09/"
xmlns:str="http://crd.gov.pl/xml/schematy/struktura/2009/11/16/"
xmlns:wnio="http://test.epuap.gov.pl/FeResourceServlet/wzor_lokalny/InstytucjaPubliczna/Deklaracja_DT/"
xmlns:xforms="http://www.w3.org/2002/xforms"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xxforms="http://orbeon.org/oxf/xml/xforms"
xsi:schemaLocation="http://test.epuap.gov.pl/FeResourceServlet/wzor_lokalny/InstytucjaPubliczna/Deklaracja_DT/ http://test.epuap.gov.pl/FeResourceServlet/wzor_lokalny/InstytucjaPubliczna/Deklaracja_DT/schemat.xsd">
<wnio:OpisDokumentu><str:CID>[email protected]</str:CID><meta:Identyfikator typIdentyfikatora="idFormularza"><meta:Wartosc>InstytucjaPubliczna/Deklaracja podatku od środków transportowych DT-1</meta:Wartosc></meta:Identyfikator></wnio:OpisDokumentu>
<wnio:Naglowek>
<wnio:KodFormularza kodSystemowy="DT-1(3)"/>
<wnio:WariantFormularza>3</wnio:WariantFormularza>
<wnio:Rok>2008</wnio:Rok>
<wnio:ObowiazekSkladaniaDeklaracji>1</wnio:ObowiazekSkladaniaDeklaracji>
<wnio:MiejsceSkladania/>
</wnio:Naglowek>
<wnio:Podatnik>
<wnio:OsobaPrawna>
<wnio:NIP>345678901</wnio:NIP>
<wnio:PelnaNazwa>Testowa Instytucja Publiczna</wnio:PelnaNazwa>
<wnio:NazwaSkrocona/>
<wnio:REGON>234512345</wnio:REGON>
</wnio:OsobaPrawna>
<wnio:AdresSiedzibyZamieszkania>
<wnio:KodKraju>PL</wnio:KodKraju>
<wnio:Wojewodztwo>ŚLĄSKIE</wnio:Wojewodztwo>
<wnio:Powiat>Katowice</wnio:Powiat>
<wnio:Gmina>Katowice (gmina miejska)</wnio:Gmina>
<wnio:Ulica>ul. Mikołowska</wnio:Ulica>
<wnio:NrDomu>100</wnio:NrDomu>
<wnio:NrLokalu>924</wnio:NrLokalu>
<wnio:Miejscowosc>Katowice (miasto)</wnio:Miejscowosc>
<wnio:KodPocztowy>40-065</wnio:KodPocztowy>
<wnio:Poczta>Katowice</wnio:Poczta>
</wnio:AdresSiedzibyZamieszkania>
</wnio:Podatnik>
<wnio:PozycjeSzczegolowe>
<wnio:D.1>
<wnio:P_22/>
<wnio:P_23/>
</wnio:D.1>
<wnio:D.2>
<wnio:P_28/>
<wnio:P_29/>
</wnio:D.2>
<wnio:P_82/>
<wnio:P_83/>
</wnio:PozycjeSzczegolowe>
<wnio:Zalacznik_DT-1A>
<wnio:P_1/>
<wnio:P_2/>
<wnio:P_22>1</wnio:P_22>
</wnio:Zalacznik_DT-1A>
<wnio:Zalacznik_DT-1A>
<wnio:P_1/>
<wnio:P_2/>
<wnio:P_22>3</wnio:P_22>
</wnio:Zalacznik_DT-1A>
</wnio:Deklaracja>and an overloaded function:
function dodaj_DT1_z_xml(p_dt1 in out nocopy XMLType) return number as
r_dt1 POD_SRTR_DT1%rowtype;
r_dt1a POD_SRTR_DT1A%rowtype;
r_sdek POD_SRTR_DEKLARACJE%rowtype;
l_wariantDT1 number(2);
l_xosf XMLType;
l_xosp XMLType;
l_xadr XMLType;
l_xszcz XMLType;
l_zal XMLType;
l_z XMLType;
l_zno pls_integer;
l_xml XMLType;
l_ns varchar2(10);
-- xmlns:wnio="http://test.epuap.gov.pl/FeResourceServlet/wzor_lokalny/InstytucjaPubliczna/Deklaracja_DT/"
begin
if --p_dt1.existsNode('/wnio:Deklaracja')=0 and
p_dt1.existsNode('/Deklaracja','xmlns:wnio="http://test.epuap.gov.pl/FeResourceServlet/wzor_lokalny/InstytucjaPubliczna/Deklaracja_DT/"')=0
then return -2; end if;
if p_dt1.existsNode('/wnio:Deklaracja')=1 then
l_ns := 'wnio:';
end if;
if p_dt1.existsNode('/'||l_ns||'Deklaracja')=0 then return -2; end if;
if p_dt1.existsNode('/'||l_ns||'Deklaracja/'||l_ns||'Naglowek')=0 then return -3; end if;
if p_dt1.existsNode('/'||l_ns||'Deklaracja/'||l_ns||'Podatnik')=0 then return -4; end if;
if p_dt1.existsNode('/'||l_ns||'Deklaracja/'||l_ns||'PozycjeSzczegolowe')=0 then return -5; end if;
return 1;
end;
function dodaj_DT1_z_xml(p_dt1 in out nocopy CLOB) return number is
l_xml XMLType;
begin
l_xml := XMLType(p_dt1);
return dodaj_DT1_z_xml(l_xml);
end dodaj_DT1_z_xml;I run the code:declare
v_clob CLOB;
v_out number;
begin
SELECT dok_content INTO v_clob FROM EPUAP.epuap_dokumenty WHERE dok_id=13;
v_out := POD_PCK_SRTR_DT1.DODAJ_DT1_Z_XML(v_clob);
DBMS_OUTPUT.put_line(v_out);
end;the output is -2. What must I change to find the node "wnio:Deklaracja"? I'm newbie in XML and namespaces :(
Help me, please...Thanks, BluShadow!
added:
What if the structure of the XML is not always the same? Suppose that <aa:USER_INFO> sometimes contains subnode <aa:AUX>.
WITH t as (select XMLTYPE('
<RECSET xmlns:aa="http://www.w3.org">
<aa:REC>
<aa:COUNTRY>1</aa:COUNTRY>
<aa:POINT>1800</aa:POINT>
<aa:USER_INFO>
<aa:USER_ID>1</aa:USER_ID>
<aa:TARGET>28</aa:TARGET>
<aa:STATE>6</aa:STATE>
<aa:TASK>12</aa:TASK>
<aa:AUX>
<aa:AUX1>111</aa:AUX1> <aa:AUX2>222</aa:AUX2>
</aa:AUX>
</aa:USER_INFO>
<aa:USER_INFO>
<aa:USER_ID>5</aa:USER_ID>
<aa:TARGET>19</aa:TARGET>
<aa:STATE>1</aa:STATE>
<aa:TASK>90</aa:TASK>
</aa:USER_INFO>
</aa:REC>
<aa:REC>
<aa:COUNTRY>2</aa:COUNTRY>
<aa:POINT>2400</aa:POINT>
<aa:USER_INFO>
<aa:USER_ID>3</aa:USER_ID>
<aa:TARGET>14</aa:TARGET>
<aa:STATE>7</aa:STATE>
<aa:TASK>5</aa:TASK>
<aa:AUX>
<aa:AUX1>333</aa:AUX1>
</aa:AUX>
</aa:USER_INFO>
</aa:REC>
</RECSET>') as xml from dual)
-- END OF TEST DATA
select x.country, x.point, y.user_id, y.target, y.state, y.task, z.aux1, z.aux2
from t
,XMLTABLE(XMLNAMESPACES('http://www.w3.org' as "aa"),
'/RECSET/aa:REC'
PASSING t.xml
COLUMNS country NUMBER PATH '/aa:REC/aa:COUNTRY'
,point NUMBER PATH '/aa:REC/aa:POINT'
,user_info XMLTYPE PATH '/aa:REC/*'
) x
,XMLTABLE(XMLNAMESPACES('http://www.w3.org' as "aa"),
'/aa:USER_INFO'
PASSING x.user_info
COLUMNS user_id NUMBER PATH '/aa:USER_INFO/aa:USER_ID'
,target NUMBER PATH '/aa:USER_INFO/aa:TARGET'
,state NUMBER PATH '/aa:USER_INFO/aa:STATE'
,task NUMBER PATH '/aa:USER_INFO/aa:TASK'
,aux XMLTYPE PATH '/aa:USER_INFO/*'
) y
,XMLTABLE(XMLNAMESPACES('http://www.w3.org' as "aa"),
'/aa:AUX'
PASSING y.aux
COLUMNS aux1 NUMBER PATH '/aa:AUX/aa:AUX1'
,aux2 NUMBER PATH '/aa:AUX/aa:AUX2'
) z;This query returns only 2 rows. I'd like to have 3 rows returned :) 3 rows are returned when I add an empty <aa:AUX> node to the data but I wants to have 3 rows when the <aa:AUX> is missed also. -
hi
how to read and edit xml file using jdom, i tried using
javax.xml.transform.*; // JAXP
javax.xml.transform.dom.DOMSource; //
i can read xml file . but iam not able to edit the xml file.
how to achive this1. Select nodes to be modified with the JDom XPath class selectSingleNode and selectNodes methods.
SAXBuilder saxBuilder=new SAXBuilder("org.apache.xerces.parsers.SAXParser");
org.jdom.Document jdomDocument=saxBuilder.build(new File("c:/input.xml"));
org.jdom.Element node= (org.jdom.Element)(XPath.selectSingleNode(jdomDocument,"/root/node"));
2. Modify the node values with the Element class setter methods.
3. Output document with the XMLOutputer class. -
How do I get values from a resultset using JDOM?
I am very new to this and I dont have the slightest idea how to retrive values from a result and create a XML tree using JDOM
Can someone please help me?
I have the JDOM Parser installed on my machine.
This is my code so far:
import org.jdom.input.*;
import org.jdom.*;
import java.sql.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Display extends HttpServlet {
public void doPost(HttpServletRequest rq, HttpServletResponse rs) throws IOException, ServletException
rs.setContentType("text/html");
PrintWriter out = rs.getWriter();
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
Connection listcars_con = DriverManager.getConnection("jdbc:odbc:Cars");
Statement listcars_statement = listcars_con.createStatement();
ResultSet listcars_rs = listcars_statement.executeQuery("Select CarNr FROM cars");
//Load the appropriate driver, establish a connection and create a statement which is used to execute the query that returns all the
//columns from the cars table
Document doc = new Document(new Element("rootElement"));
listcars_statement.close();
listcars_con.close();
//close the connections
catch (ClassNotFoundException cnfe)
System.err.println(cnfe);
} catch (SQLException sqlex) {
System.err.println(sqlex);
} catch (Exception er) {
er.printStackTrace();
} //closing bracket for doPost method
} //closing bracket for class definitionResultSet object has nothing to do with XML... so the only way to it
is just read each row/column in your resultset and then create
XML record based on structure of your resultset - annoying job...
Also you can try to use tool developed by Oracle which based
on SQL select statement parses resultset into XML...
Paul
Maybe you are looking for
-
How can i use iobd2 with bluetooth device obd2
Hi, I want to connect an obd2 bluetooth device with my iphone 4S/ipad 3 via bluetooth. My Apple device can't find the bluetooth device. What can I do to solve this Problem? I don't want to buy an android tablet or phone.
-
my ipod touch 5th generation has been locked out. it now says on itunes that i need to know my passcode in order to sign in with itunes on the computer..but i dont know my passcode so i cannot get on my itunes to reset my ipod because it said i need
-
ITunes update always fails on Windows 7 x64
I have had this problem for over a year on my Windows 7 64-bit system. Whenever there is a new version of iTunes, I try and update using the iTunes Software Update notification that pops up. This always fails to install. I am then forced to uninst
-
List of BlackBerry's And Specs?
Hey everyone, Im writing a technical report for one of my school programs, and I have chosen to write about how BlackBerry's have advanced through the years. I was wondering if there was a good place to find older phone specs, i.e. processor power, r
-
Why creative cloud installer won't work?
Hi, I am trying to download the trial for premiere pro and I'm having a lot of trouble. I click download and I have the creative cloud installer in finder. When I open it, creative cloud opens for about three seconds with a loading sign but then disa