how to using DOM importNode without DOMException - java

I want to copy an node to a different Documentg, but it always has DOMException about
org.apache.harmony.xml.dom.NodeImpl.setNameNS(NodeImpl.java:227)
here is my code
private String getString(Node seqNode) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = factory.newDocumentBuilder();
Document doc = docBuilder.newDocument();
Element root = doc.createElement("Test");
doc.appendChild(root);
/* following line will cause DOMException */
doc.importNode(seqNode, true);
...
...
} catch (Exception e) {
}
}
where seqNode belongs to other Document
does any body has idea about this issue? :-)

I ran into this problem too. I was getting this exception when calling either importNode() or cloneNode(). And BTW, the XML I was parsing/generating was not using namespaces.
It seems that the DOM parser (from Apache Harmony) that is included in Android is buggy. See this link: Issue 2735: Harmony DOM implementation is buggy and noncompliant. Everything works fine if the same code is executed using plain Java 1.6 (which isn't based on Harmony of course).
I tried setting setNamespaceAware(true) on the DocumentBuilder, but this did not help.
Eventually, I gave up and worked around the issue by using adoptNode() rather than importNode(). This is kind of incestuous, because it is stealing a node from one Document tree and putting it into another. But in my case, the first Document tree was only temporary, so I could do things this way.

I'm guessing, but it seems that you are trying to import node with namespace defined, where your target document does not have this namespace declared.
So, what namespaces are declared in source document? Did you declare any namespaces in target document?

The input is a smil String shown below:
<smil>
<head>
<layout>
<root-layout height="720" width="1280"/>
<transition id="fade" type="fade" subtype="crossfade" dur="1s"/>
<region id="_33_32_bkgd_image" left="0" top="0" width="1280" height="720" background-color="#c12121" showBackground="whenActive" z-index="0"></region>
<region id="_33_32_I001" left="380" top="27" width="405" height="352" z-index="1"></region><region id="_33_32_I002" left="0" top="365" width="354" height="354" z-index="2"></region>
</layout>
</head>
<body>
<seq begin="wallclock(2011-09-22T01:52:00)" end="wallclock(2011-09-23T00:00:00)">
<par dur="10s" xml:id="32" repeatCount="1">
<brush color="#c12121" region="_33_32_bkgd_image"></brush>
<seq repeatCount="indefinite">
<img xml:id="30" region="_33_32_I001" src="http://127.0.0.1/Service/User/2_user/Media/Image/30_image.jpg?JFBukihsTu" dur="5s" fit="meet" regPoint="center" regAlign="center">
<metadata xml:id="meta-rdf">
<meta name="MD5" content="7c8b59b28ea2247f20bc538dcb7108f3"></meta><meta name="width" content="531"></meta><meta name="height" content="720"></meta></metadata></img>
<img xml:id="27" region="_33_32_I001" src="http://127.0.0.1/Service/User/2_user/Media/Image/27_image.jpg?jTqCMuIxsX" dur="5s" fit="meet" regPoint="center" regAlign="center">
<metadata xml:id="meta-rdf">
<meta name="MD5" content="db51409f243f79c566811d1b307a77a1"></meta><meta name="width" content="427"></meta><meta name="height" content="602"></meta></metadata></img>
</seq>
</par>
</seq>
</body>
</smil>
and the original Document is generated by:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document dom = builder.parse(new ByteArrayInputStream(smil.getBytes()));
and the seqNode represents the "seq" node (the child of body tag)
I want to copy "seq" and all of it's childs to new Document

Related

How to use java to get element of xml document, but in xml string format?

I have read some links on parsing xml document like below:
<inventory>
<book year="2000">
<title>Snow Crash</title>
<author>Neal Stephenson</author>
<publisher>Spectra</publisher>
<isbn>0553380958</isbn>
<price>14.95</price>
</book>
<book year="2005">
<title>Burning Tower</title>
<author>Larry Niven</author>
<author>Jerry Pournelle</author>
<publisher>Pocket</publisher>
<isbn>0743416910</isbn>
<price>5.99</price>
</book>
<!-- more books... -->
</inventory>
using DOM parsing:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(<uri_as_string>);
XPathFactory xPathfactory = XPathFactory.newInstance();
XPath xpath = xPathfactory.newXPath();
XPathExpression expr = xpath.compile(<xpath_expression>);
however, their purpose are mostly to get VALUE of some node(s) by tag or by attribute from the document.
My purpose is to get the entire XML STRING of the node(s) back. For example, using Xpath /inventory/book[#year='2005'], i want to get the following xml back in a single string, i.e.
<book year="2005">
<title>Burning Tower</title>
<author>Larry Niven</author>
<author>Jerry Pournelle</author>
<publisher>Pocket</publisher>
<isbn>0743416910</isbn>
<price>5.99</price>
</book>
What is the API used for this purpose? And do i even need the DOM parsing in this case? Thanks,
COMMENT:
Maybe I should emphasize that I am asking this question as a XML related one, not a text file processing question. Concepts like 'tag', 'attribute', 'Xpath' still apply. The DOM model is not totally irrelevant. It's just that instead of getting the 'element' or value of a node, i want to get the whole node.
The given answers can not solve problems like: how to get a node in xml string format, given the node's Xpath representation, such as //book or /inventory/book[1]?
DOM parsers are designed to get values from the them not for actual file content.
You can use a simple file reader instead of XML.
Read line by line using a simple FileReader and check the line for the Condition and if the condition is met start the read content to concat as you want until the End of the node .
You can do it as
if(lineReadFromFile=="Your String Condition"){
//collect the desired file content here untill the end of the Node is found
}
You can simply read XML from file (consider it to be a normal text file) using FileReader. Simple apply the condition for example :
if(line.equals("<book year="2005"><title>Burning Tower</title>")) {
// retrieve/save the required content
}

java XML parsing, the markup must be well-formed

My XML file is in the following format:
<top>
<name></name>
<title></title>
<time></time>
</top>
<top>
...
</top>
<top>
...
</top>
I write the following code to read the xml file:
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(new File(QUERY_FILE)); //LINE (*)
doc.getDocumentElement().normalize();
NodeList nList = doc.getElementsByTagName("top");
But the problem is I get error as at line (*):
The markup in the document following the root element must be
well-formed.
It seems that error is because I have multiple root elements in the xml file. One solution could be I add maybe <doc></doc> outside all <top> elements. But is there any other way that I can directly read in such XML file as element arrays?
You can try to isolate each<top> element and trying to parse them separately, but that's a more troublesome solution than just wrapping <doc></doc> around the xml content..
One thing I've done in the past is instead of putting the root tags in the file itself, I just read the text into a string, and wrap the <doc></doc> tags around the string before I load the XML.
You are add this line for well-formed :
<?xml version="1.0" encoding="UTF-8"?> <!-- this line-->
<top>
<name></name>
<title></title>
<time></time>
</top>
Use this page to see if your document is correct, since it is the one that sets the standard for this metalanguage.
http://validator.w3.org/#validate_by_input
Validate xml dtd, etc ..
The World Wide Web Consortium (W3C) is the main international standards organization for the World Wide Web (abbreviated WWW or W3).| Font Wikipedia

Trouble Creating org.w3c.dom.Document from xml string in java

I am trying to create an org.w3c.dom.Document object from an xml string. I have followed what many have suggested in other questions but the document ends up empty. What is wrong with the following code?
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(new InputSource(new StringReader(response.getResponseText())));
And the xml text in the string looks like the following (this comes from response.getResponseText())
<s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing">
<s:Header>
<a:Action s:mustUnderstand="1">http://www.blah.com/ns/2006/05/01/webservices/123/TokenManagement_1/CreateServiceToken_1_Reply</a:Action>
<CacheResponse xsi:type="DoNotStoreCacheResponse" xmlns="http://www.blah.com/ns/2008/03/01/webservices/123/Cache_1" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<Date>2012-09-04T15:35:06.8116593Z</Date>
<DoNotStore />
</CacheResponse>
<a:RelatesTo>ba04425d-d93e-4a70-a134-ab8e29d5345c}</a:RelatesTo>
</s:Header>
<s:Body>
<CreateServiceToken_Response_1 xmlns="http://www.blah.com/ns/2006/05/01/webservices/123/TokenManagement_1" xmlns:global="http://www.blah.com/ns/2006/05/01/webservices/123/Common_1">
<Expiration>2012-09-04T17:04:19.1834228Z</Expiration>
<global:Token>3DEC2723A01047D1590544CBA5BA1E30326535E609DC1E6FAC5C659BC3B8A693BB054834A58B235037ED830CD05784DB176A62309AEB4B608C6F0B5B3F13ADE0EC56BE9F822ACFA3B549D4427D89BF030BFF48BA671DCAEB49940EFEBDEBFB71</global:Token>
</CreateServiceToken_Response_1>
</s:Body>
Can anyone see what is wrong with my code? I ultimately just want to run a couple of xpath queries on the document...
I would suggest to start with setting docFactory.setNamespaceAware(true);, otherwise the parsing, the DOM built and the XPath implementation will not be able to work with XML with namespaces as you have posted.

how to insert value into xml?

I am really new to XML and JDOM so I have a noob question, sorry for that. I have a XML file and I want to insert value into it. My XML file is like that;
<?xml version="1.0"?>
<message>
<header>
<messageType> </messageType>
<sendFrom> </sendFrom>
<HostName> </HostName>
<sendTo> </sendTo>
<receiverName> </receiverName>
<date> </date>
</header>
<body>
</body>
</message>
So what I want is for example is to add value between <sendTo> </sendTo> and also I want to add <A> data </A> between <body> </body>. Can you please tell me how to do that ?
Thanks a lot.
http://www.cafeconleche.org/books/xmljava/chapters/ch14s04.html
http://www.java2s.com/Code/Java/XML/MakeupandwriteanXMLdocumentusingDOM.htm
If you use dom,you can do it as follows;
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder.parse(inputFile);
Node messageType= doc.getElementsByTagName("messageType").item(0);//zero tells the order in the xml
messageType.setTextContent("SMS");
I'd recommend using XStream for XML handling.
Here, is the link to a 2 minute tutorial: http://x-stream.github.io/tutorial.html

Java DOM, namespace / version problem

Im in the process of creating XML as a Node for a RMI program I am developing but I have run across a problem. I can create the XML using DOM but I am struggling to add namespace and version to the top of my XML. I have tried using setAttribute and setAttributeNS but at the moment lost in what else I can do.
The java code to create the element is:
DocumentBuilderFactory dbfac = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = dbfac.newDocumentBuilder();
Document doc = docBuilder.newDocument();
Node root = doc.createElement("Request");
doc.appendChild(root);
//code ommited
The result I get currently is:
<Request>
<Identification>
<UserID>user</UserID>
<Password>pass</Password>
</Identification>
</Request>
In the request section I need it to look like:
<Request xsi:noNamespaceSchemaLocation="URL" Version="1.0">
Any help will be appreciated to help solve this issue!
Thanks
I think you'd want something like:
...
Element root = doc.createElement("Request");
root.setAttributeNS("http://www.w3.org/2001/XMLSchema-instance", "xsi:noNamespaceSchemaLocation", "URL");
root.setAttribute("Version", "1.0");
doc.appendChild(root);
...
Defining root as an Element gives you the .setAttribute* methods.
This would give you
<Request Version="1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="URL"/>
I know that includes a bit more, but the xmlns:xsi attribute is needed so that the xsi namespace is defined.

Categories