Import node DOM - java

I am trying to import node from one doc to another:
DocumentBuilder db = dbf.newDocumentBuilder();
DocumentBuilder db2 = dbf2.newDocumentBuilder();
Document doc1 =parser.buildDoc(message.getBytes("UTF-8"));
Document doc2 = db2.parse(new FileInputStream(new File("C:\\Temp\\workspace2\\Resource2Q\\xml_template.xml")));
NodeList list = doc1.getElementsByTagName("Form");
for(int i=0; i<list.getLength(); i++)
{
Element element = (Element) list.item(i);
Node copiedNode = doc1.importNode(element, true);
doc2.getDocumentElement().appendChild(copiedNode); ...
The last line of code gives me: "WRONG_DOCUMENT_ERR: A node is used in a different document than the one that created it".
Why is this happening? i am importing the node.

Node copiedNode = doc1.importNode(element, true);
should be
Node copiedNode = doc2.importNode(element, true);
The node comes from doc1, and you want to import it into doc2. Not into doc1, where it already comes from.

Related

XPATH evaluation against child node

I have xml as follows,
<students>
<Student><age>23</age><id>2000</id><name>PP2000</name></Student>
<Student><age>23</age><id>1000</id><name>PP1000</name></Student>
</students>
I have 2 xpaths Template XPATH = students/Student will be the template nodes, but I cannot hard code this xpath, because it will change for other XMLs, and XML is pretty dynamic, can expand (but with the same base XPATHs) So if I evaluate one more XPATH using the template node, I'm using the following code,
XPath xpathResource = XPathFactory.newInstance().newXPath();
Document xmlDocument = //creating document;
NodeList nodeList = (NodeList)xpathResource.compile("//students/Student").evaluate(xmlDocument, XPathConstants.NODESET);
for (int nodeIndex = 0; nodeIndex < nodeList.getLength(); nodeIndex++) {
Node currentNode = nodeList.item(nodeIndex);
String xpathID = "//students/Student/id";
String xpathName = "//students/Student/name";
NodeList childID = (NodeList)xpathResource.compile(xpathID).evaluate(currentNode, XPathConstants.NODESET);
NodeList childName = (NodeList)xpathResource.compile(xpathName).evaluate(currentNode, XPathConstants.NODESET);
System.out.println("node ID " +childID.item(0).getTextContent());
System.out.println("node Name " +childName.item(0).getTextContent());
}
Now the problem is, this for loop will execute for 2 times, but both time I'm getting 2000 , PP2000 as ID value. Is there any way to iterate to the child node with generic XPATH against a node. I cannot go generic XPATH against the whole XMLDocument, I have some validation to do. I want to use XML nodelist as result set rows, so that I can validate the XML value and do my stuff.
XPath xpathResource = XPathFactory.newInstance().newXPath();
Document xmlDocument = //creating document;
NodeList nodeList = (NodeList)xpathResource.compile("//students/Student/id").evaluate(xmlDocument, XPathConstants.NODESET);
for (int nodeIndex = 0; nodeIndex < nodeList.getLength(); nodeIndex++) {
Node currentNode = nodeList.item(nodeIndex);
System.out.println("node " +currentNode.getTextContent());
}

XML - Extract One tag Value

I have to extract tag value from an xml Document that contains a single tag like below:
<error>Permission denied</error>
i have tried:
String xmlRecords = "<error>Permission denied</error>"
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xmlRecords));
Document doc = db.parse(is);
Node nodes = doc.getFirstChild();
String = nodes.getNodeValue();
but it dont works.
How can i do it ?
Use doc.getDocumentElement().getTextContent() to get the string Permission denied.
With DOM it´s util to know the structure of the XML document, and which node level are you looking for.
After get Document, you can use document.getElementsByTagName("root") to look for the root or father tags, and get the childs as a list to look for the item. Something like this:
NodeList listresults = document.getElementsByTagName('father/root element string');
NodeList nl = listresults.item(0).getChildNodes();
// Recorremos los nodos
for (int temp = 0; temp < nl.getLength(); temp++) {
Node node = nl.item(temp);
// Check if it is a node
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element element = (Element) node;
if(element.getNodeName().equals("error")){
// check the element
}
}
}
I hope this helps you.
just try following code.
String value = nodes.getTextContent();
You have to construct the string if you are using the above approach. You will get the string values of the tag name and content using the functions.
Tag name = nodes.getTextContent()
tag value = nodes.getLocalName()
I guess this is what you want
Element element = document.getDocumentElement();
NodeList errorTagList = element.getElementsByTagName("error");
if (errorTagList != null && errorTagList.getLength() > 0) {
NodeList errorTagSubList = errorTagList.item(0).getChildNodes();
if (errorTagSubList != null && errorTagSubList.getLength() > 0) {
String value = errorTagSubList.item(0).getNodeValue();
}
}

Android - Reading XML issues

I've got an app that consumes a .NET web service which returns an XML string of data. I'm trying to read this XML and insert it into the local SQLite DB but I'm having some trouble. Here's a sample of the xml:
<?xml version="1.0" encoding="utf-8" ?>
<string xmlns="RemoteWebService"><OpenIssues> <Table> <IssueID>15351</IssueID> <IssueSummary>Computer keeps crashing. This continues to be a problem</IssueSummary> <LocationName>West Side</LocationName> <Status>WIP</Status> <CustomerID>89755</CustomerID> <CustomerName>West Side Computers</CustomerName> <CustomerShortName>WSC</CustomerShortName> <Notes /> <STATUS1>Work In Progress</STATUS1> <SubmittedBy>WSC - Tom Johns</SubmittedBy> <EQ_Replaced>true</EQ_Replaced></Table> </OpenIssues></string>
Using DOM, I'm trying to parse the results like so:
private void GetLatestData(String response) throws ParserConfigurationException, SAXException, IOException{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(new StringReader(response)));
//Normalize the document.
doc.getDocumentElement().normalize();
//Get Root Node.
NodeList nodeList = doc.getElementsByTagName("Table");
Node node = nodeList.item(0);
//Get Child Nodes.
for(int i = 0; i < node.getChildNodes().getLength(); i++){
IssueInfo issue = new IssueInfo();
Node tempNode = node.getChildNodes().item(i);
if(tempNode.getNodeName().equalsIgnoreCase("IssueID")){
issue.setIssueNumber(Long.parseLong(tempNode.getTextContent()));
}
if(tempNode.getNodeName().equalsIgnoreCase("IssueSummary")){
issue.setIssueNumber(Long.parseLong(tempNode.getTextContent()));
}
if(issue.getIssueNumber() > 0 && issue.getIssueSummary() != null){
creator = new IssueInfoCreator(this, DBVersion);
creator.open();
creator.InsertIssue(issue.getIssueNumber(), DateFormat.getDateInstance().format(new Date()), issue.getIssueSummary());
creator.close();
}
}
}
When I run it through the debugger, it gets "IssueID" just fine but how can I get it to pickup the next node "IssueSummary" right after that so I can insert the data at once? It seems like I need another loop somewhere, just not too sure where though.
If I understand your question correctly, this is what you may need to do.
Node node = nodeList.item(0);
Returns
<Table>
<IssueID>15351</IssueID>
<IssueSummary>Computer keeps crashing. This continues to be a problem</IssueSummary>
<Notes />
</Table>
node.getChildNodes().getLength();
Returns 3.
IssueInfo issue = new IssueInfo();
// Go through each child and find out node name and populate it.
for(int i = 0; i < node.getChildNodes().getLength(); i++){
Node tempNode = node.getChildNodes().item(i);
if(tempNode.getNodeName().equalsIgnoreCase("IssueID")){
issue.setIssueNumber(Long.parseLong(tempNode.getTextContent()));
}
if(tempNode.getNodeName().equalsIgnoreCase("IssueSummary")){
issue.setIssueNumber(Long.parseLong(tempNode.getTextContent()));
}
}
Move if logic out of the loop.
if(issue.getIssueNumber() > 0 && issue.getIssueSummary() != null){
creator = new IssueInfoCreator(this, DBVersion);
creator.open();
creator.InsertIssue(issue.getIssueNumber(), DateFormat.getDateInstance().format(new Date()), issue.getIssueSummary());
creator.close();
}
Seems like a simple DOM traversing issue.
If you can guarantee the next node is the summary, you could try using the getNextSibling() method for nodes
I modified your code so I could call it without using your classes. This is the code I used:
private static void GetLatestData(String response) {
try{
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
/* the following 2 lines help you eliminate whitespace
from your xml DOM tree */
dbf.setValidating(true);
dbf.setIgnoringElementContentWhitespace(true);
DocumentBuilder db = dbf.newDocumentBuilder();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(new StringReader(response)));
//Normalize the document.
doc.getDocumentElement().normalize();
//Get Root Node.
NodeList nodeList = doc.getElementsByTagName("Table");
Node node = nodeList.item(0);
long issueNumber;
String summary;
//Get Child Nodes.
for(int i = 0; i < node.getChildNodes().getLength(); i++){
Node tempNode = node.getChildNodes().item(i);
if(tempNode.getNodeName().equalsIgnoreCase("IssueID")){
issueNumber = (Long.parseLong(tempNode.getTextContent()));
Node summaryNode = tempNode.getNextSibling();
summary = summaryNode.getTextContent();
System.out.println(String.format("Issue # %d, Summary: %s" , issueNumber,summary));
}
}
}catch(Exception exception){
exception.printStackTrace();
}
}
and I call it like this:
GetLatestData("<OpenIssues> " +
"<Table> " +
"<IssueID>15351</IssueID>" +
"<IssueSummary>Computer keeps crashing. This continues to be a problem</IssueSummary> " +
"<Notes />" +
"</Table></OpenIssues> ");
from a simple Java class. It's working all right for me, at least. It prints out:
Issue # 15351, Summary: Computer keeps crashing. This continues to be a problem
*smacks forehead*
issue will never have more than one value set, as it's created anew for each child node.
Just swap the two lines to create issue only once:
IssueInfo issue = new IssueInfo();
for(int i = 0; i < node.getChildNodes().getLength(); i++){
...
You should probably move the final if outside the for too, so it's not executed more than once.
AND you'll need to actually set the summary in the second if not. You're setting the 'issue number' twice.
Finally found the resolution to this with the help of my coworker and some digging around. It should be noted that we changed the WebService that returned a string from DataSet.GetXml() to an XmlDocument.InnerXml. This removed the spaces in between the nodes and then we were able to move forward from there. Here's the final code we used:
public void GetLatestData(SoapPrimitive xml)throws ParserConfigurationException, SAXException, IOException{
//get the factory
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
//Using factory get an instance of document builder
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc;
//parse using builder to get DOM representation of the XML file
InputSource is = new InputSource(new StringReader(xml.toString()));
doc = db.parse(is);
//Clear out Issues table first.
creator = new IssueInfoCreator(this, DBVersion);
creator.open();
creator.ClearIssueTable();
creator.close();
NodeList nodes = doc.getElementsByTagName("Table");
for(int i = 0; i < nodes.getLength(); i++) {
IssueInfo issue = new IssueInfo();
Element e = (Element)nodes.item(i);
issue.setIssueNumber(Long.parseLong(XMLfunctions.getValue(e, "IssueID")));
issue.setIssueSummary(XMLfunctions.getValue(e, "IssueSummary"));
issue.setDateReceived(DateFormat.format("MM/dd/yyyy hh:mm:ss", System.currentTimeMillis()).toString());
if(issue.getIssueNumber() > 0 && issue.getIssueSummary() != null){
creator = new IssueInfoCreator(this, DBVersion);
creator.open();
creator.InsertIssue(issue.getIssueNumber(), issue.getDateReceived(), issue.getIssueSummary());
creator.close();
}
}
}
And here is the getValue method of the XMLfuntions class:
public static String getValue(Element item, String str) {
NodeList n = item.getElementsByTagName(str);
return XMLfunctions.getElementValue(n.item(0));
}
public final static String getElementValue( Node elem ) {
Node kid;
if( elem != null){
if (elem.hasChildNodes()){
for( kid = elem.getFirstChild(); kid != null; kid = kid.getNextSibling() ){
if( kid.getNodeType() == Node.TEXT_NODE ){
return kid.getNodeValue();
}
}
}
}
return "";
}
Definately not taking credit for this, I found it here:
Programmer XR and modified it to my needs.
Hopefully this will help other people out!

Child node name in a xml

I am trying to write a piece of code that can parse any xml and print its contents. I am using DOM parser. I am able to get the name of the root tag of the xml, but cant obtain tag name of the immediate child. This can be done easily in case the node names are known by using the method 'getElementsByTagName' . Is there any way out of this dilemma ?
My code goes like this :
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(file);
doc.getDocumentElement().normalize();
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
doc.getDocumentElement().getNodeName() // this gets me the name of the root node.
Now how can i get the name of the immediate child node so that i can traverse the xml using getElementsByTagName("x").
Thanks in advance.
getChildNodes() returns all children of an element. The list will contain more then just elements so you'll have to check each child node if it is an element:
NodeList nodes = doc.getDocumentElement().getChildNodes();
for (int i = 0; i < nodes.getLength(); i++) {
Node node = nodes.get(i);
if (node instanceof Element) {
Element childElement = (Element) node;
System.out.println("tag name: " + childElement.getTagName());
}
}

XML parser gives null element

When I try to parse a XML-file, it gives sometimes a null element by the title.
I think it has to do with HTML-tags '
How can I solve this problem?
I have the follow XML-file:
<item>
<title>' Nieuwe DVD '</title>
<description>tekst, tekst tekst</description>
<link>dvd.html</link>
<category>nieuws</category>
<pubDate>Sat, 1 Jan 2011 9:24:00 +0000</pubDate>
</item>
And the follow code to parse the xml-file:
//DocumentBuilderFactory, DocumentBuilder are used for
//xml parsing
DocumentBuilderFactory dbf = DocumentBuilderFactory
.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
//using db (Document Builder) parse xml data and assign
//it to Element
Document document = db.parse(is);
Element element = document.getDocumentElement();
//take rss nodes to NodeList
element.normalize();
NodeList nodeList = element.getElementsByTagName("item");
if (nodeList.getLength() > 0)
{
for (int i = 0; i < nodeList.getLength(); i++)
{
//take each entry (corresponds to <item></item> tags in
//xml data
Element entry = (Element) nodeList.item(i);
entry.normalize();
Element _titleE = (Element) entry.getElementsByTagName(
"title").item(0);
Element _categoryE = (Element) entry
.getElementsByTagName("category").item(0);
Element _pubDateE = (Element) entry
.getElementsByTagName("pubDate").item(0);
Element _linkE = (Element) entry.getElementsByTagName(
"link").item(0);
String _title = _titleE.getFirstChild().getNodeValue();
String _category = _categoryE.getFirstChild().getNodeValue();
Date _pubDate = new Date(_pubDateE.getFirstChild().getNodeValue());
String _link = _linkE.getFirstChild().getNodeValue();
//create RssItemObject and add it to the ArrayList
RssItem rssItem = new RssItem(_title, _category, _pubDate, _link);
rssItems.add(rssItem);
conn.disconnect();
}
Don't use getFirstElement when you really want getTextContent.

Categories