Skip to main content
Code Review

Return to Question

Notice removed Draw attention by Community Bot
Bounty Ended with ferada's answer chosen by Community Bot
Link documentation.
Source Link
ferada
  • 11.4k
  • 25
  • 65

I have a need to extract a part of XML tree (everything under root) and convert it to a string. (The result string will be later pasted inside XSL-FO XSLT file.)

The above functionality is a part of Java 6 application. I use org.w3c.domorg.w3c.dom and javax.xml.transformjavax.xml.transform. Still in the debugging process.

// ...
// Extract all child notes of root from foDoc
// and convert the result to String
Node root = foDoc.getElementsByTagNameNS("*","root").item(0);
NodeList body = root.getChildNodes();
String foResult;

try {
 foResult = nodeListToString(body);
} catch (TransformerException e) {
 logger.error(e);
 foResult = "";
}
// ...

private static String nodeListToString(NodeList nodes) throws TransformerException {
 StringBuilder result = new StringBuilder();
 int len = nodes.getLength();
 for(int i = 0; i < len; ++ i) {
 result.append(nodeToString(nodes.item(i)));
 }
 return result.toString();
}
private static String nodeToString(Node node) throws TransformerException {
 StringWriter buf = new StringWriter();
 Transformer xform = TransformerFactory.newInstance().newTransformer();
 xform.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
 xform.transform(new DOMSource(node), new StreamResult(buf));
 return(buf.toString());
}

My questions:

  1. Assuming that there will always be exactly one root node, how reliable is my code in extracting everything under it?

  2. Am I missing any edge cases?

  3. Anything just plain wrong?

Please disregard any exception handling inadequacies.

Edited on 10/06/2015

Since my originally posted code had a "just plain wrong" part, I've fixed it. There were no answers based on the old code.

I have a need to extract a part of XML tree (everything under root) and convert it to a string. (The result string will be later pasted inside XSL-FO XSLT file.)

The above functionality is a part of Java 6 application. I use org.w3c.dom and javax.xml.transform. Still in the debugging process.

// ...
// Extract all child notes of root from foDoc
// and convert the result to String
Node root = foDoc.getElementsByTagNameNS("*","root").item(0);
NodeList body = root.getChildNodes();
String foResult;

try {
 foResult = nodeListToString(body);
} catch (TransformerException e) {
 logger.error(e);
 foResult = "";
}
// ...

private static String nodeListToString(NodeList nodes) throws TransformerException {
 StringBuilder result = new StringBuilder();
 int len = nodes.getLength();
 for(int i = 0; i < len; ++ i) {
 result.append(nodeToString(nodes.item(i)));
 }
 return result.toString();
}
private static String nodeToString(Node node) throws TransformerException {
 StringWriter buf = new StringWriter();
 Transformer xform = TransformerFactory.newInstance().newTransformer();
 xform.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
 xform.transform(new DOMSource(node), new StreamResult(buf));
 return(buf.toString());

My questions:

  1. Assuming that there will always be exactly one root node, how reliable is my code in extracting everything under it?

  2. Am I missing any edge cases?

  3. Anything just plain wrong?

Please disregard any exception handling inadequacies.

Edited on 10/06/2015

Since my originally posted code had a "just plain wrong" part, I've fixed it. There were no answers based on the old code.

I have a need to extract a part of XML tree (everything under root) and convert it to a string. (The result string will be later pasted inside XSL-FO XSLT file.)

The above functionality is a part of Java 6 application. I use org.w3c.dom and javax.xml.transform. Still in the debugging process.

// ...
// Extract all child notes of root from foDoc
// and convert the result to String
Node root = foDoc.getElementsByTagNameNS("*","root").item(0);
NodeList body = root.getChildNodes();
String foResult;
try {
 foResult = nodeListToString(body);
} catch (TransformerException e) {
 logger.error(e);
 foResult = "";
}
// ...

private static String nodeListToString(NodeList nodes) throws TransformerException {
 StringBuilder result = new StringBuilder();
 int len = nodes.getLength();
 for(int i = 0; i < len; ++ i) {
 result.append(nodeToString(nodes.item(i)));
 }
 return result.toString();
}
private static String nodeToString(Node node) throws TransformerException {
 StringWriter buf = new StringWriter();
 Transformer xform = TransformerFactory.newInstance().newTransformer();
 xform.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
 xform.transform(new DOMSource(node), new StreamResult(buf));
 return(buf.toString());
}

My questions:

  1. Assuming that there will always be exactly one root node, how reliable is my code in extracting everything under it?

  2. Am I missing any edge cases?

  3. Anything just plain wrong?

Please disregard any exception handling inadequacies.

Edited on 10/06/2015

Since my originally posted code had a "just plain wrong" part, I've fixed it. There were no answers based on the old code.

Notice added Draw attention by PM 77-1
Bounty Started worth 50 reputation by PM 77-1
Additional code fix
Source Link
PM 77-1
  • 597
  • 2
  • 11
  • 24

I have a need to extract a part of XML tree (everything under root) and convert it to a string. (The result string will be later pasted inside XSL-FO XSLT file.)

The above functionality is a part of Java 6 application. I use org.w3c.dom and javax.xml.transform. Still in the debugging process.

 // ...
 // Extract all child notes of root from foDoc
 // and convert the result to String
 Node root = foDoc.getElementsByTagNameNS("*","root").item(0);
 NodeList body = foDocroot.getChildNodes();
 String foResult;
 
 try {
 foResult = nodeListToString(body);
 } catch (TransformerException e) {
 logger.error(e);
 foResult = "";
 }
 // ...

private static String nodeListToString(NodeList nodes) throws TransformerException {
 StringBuilder result = new StringBuilder();
 int len = nodes.getLength();
 for(int i = 0; i < len; ++ i) {
 result.append(nodeToString(nodes.item(i)));
 }
 return result.toString();
}
private static String nodeToString(Node node) throws TransformerException {
 StringWriter buf = new StringWriter();
 Transformer xform = TransformerFactory.newInstance().newTransformer();
 xform.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
 xform.transform(new DOMSource(node), new StreamResult(buf));
 return(buf.toString());

My questions:

  1. Assuming that there will always be exactly one root node, how reliable is my code in extracting everything under it?

  2. Am I missing any edge cases?

  3. Anything just plain wrong?

Please disregard any exception handling inadequacies.

Edited on 10/06/2015

Since my originally posted code had a "just plain wrong" part, I've fixed it. There were no answers based on the old code.

I have a need to extract a part of XML tree (everything under root) and convert it to a string. (The result string will be later pasted inside XSL-FO XSLT file.)

The above functionality is a part of Java 6 application. I use org.w3c.dom and javax.xml.transform. Still in the debugging process.

 // ...
 // Extract all child notes of root from foDoc
 // and convert the result to String
 NodeList body = foDoc.getChildNodes();
 String foResult;
 
 try {
 foResult = nodeListToString(body);
 } catch (TransformerException e) {
 logger.error(e);
 foResult = "";
 }
 // ...

private static String nodeListToString(NodeList nodes) throws TransformerException {
 StringBuilder result = new StringBuilder();
 int len = nodes.getLength();
 for(int i = 0; i < len; ++ i) {
 result.append(nodeToString(nodes.item(i)));
 }
 return result.toString();
}
private static String nodeToString(Node node) throws TransformerException {
 StringWriter buf = new StringWriter();
 Transformer xform = TransformerFactory.newInstance().newTransformer();
 xform.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
 xform.transform(new DOMSource(node), new StreamResult(buf));
 return(buf.toString());

My questions:

  1. Assuming that there will always be exactly one root node, how reliable is my code in extracting everything under it?

  2. Am I missing any edge cases?

  3. Anything just plain wrong?

Please disregard any exception handling inadequacies.

Edited on 10/06/2015

Since my originally posted code had a "just plain wrong" part, I've fixed it. There were no answers based on the old code.

I have a need to extract a part of XML tree (everything under root) and convert it to a string. (The result string will be later pasted inside XSL-FO XSLT file.)

The above functionality is a part of Java 6 application. I use org.w3c.dom and javax.xml.transform. Still in the debugging process.

 // ...
 // Extract all child notes of root from foDoc
 // and convert the result to String
 Node root = foDoc.getElementsByTagNameNS("*","root").item(0);
 NodeList body = root.getChildNodes();
 String foResult;
 
 try {
 foResult = nodeListToString(body);
 } catch (TransformerException e) {
 logger.error(e);
 foResult = "";
 }
 // ...

private static String nodeListToString(NodeList nodes) throws TransformerException {
 StringBuilder result = new StringBuilder();
 int len = nodes.getLength();
 for(int i = 0; i < len; ++ i) {
 result.append(nodeToString(nodes.item(i)));
 }
 return result.toString();
}
private static String nodeToString(Node node) throws TransformerException {
 StringWriter buf = new StringWriter();
 Transformer xform = TransformerFactory.newInstance().newTransformer();
 xform.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
 xform.transform(new DOMSource(node), new StreamResult(buf));
 return(buf.toString());

My questions:

  1. Assuming that there will always be exactly one root node, how reliable is my code in extracting everything under it?

  2. Am I missing any edge cases?

  3. Anything just plain wrong?

Please disregard any exception handling inadequacies.

Edited on 10/06/2015

Since my originally posted code had a "just plain wrong" part, I've fixed it. There were no answers based on the old code.

Posted a fixed code
Source Link
PM 77-1
  • 597
  • 2
  • 11
  • 24

The above functionality is a part of Java 6 application. I use org.w3c.dom, javax.xml.transform, and javax.xml.xpathtransform. Still in the debugging process.

 // ...
 // Extract the required nodes
all child notes of //root from Document foDoc
 // to String foResult
 and convert XPathFactorythe xPathfactoryresult =to XPathFactory.newInstance();String
 XPathNodeList xpathbody = xPathfactoryfoDoc.newXPathgetChildNodes();
 XPathExpression expr;
 NodeList body = null;
 String foResult;
 String xp = "/*[local-name()='root']//descendant::*";
 
 try {
 expr = xpath.compile(xp);
 body = (NodeList) expr.evaluate(foDoc, XPathConstants.NODESET);
 foResult = nodeListToString(body);
 } catch (XPathExpressionException e) {
 logger.error(e);
 foResult = "";
 } catch (TransformerException e) {
 logger.error(e);
 foResult = "";
 }
 // ...

Please disregard any exception handling inadequacies.

Edited on 10/06/2015

Since my originally posted code had a "just plain wrong" part, I've fixed it. There were no answers based on the old code.

The above functionality is a part of Java 6 application. I use org.w3c.dom, javax.xml.transform, and javax.xml.xpath. Still in the debugging process.

 // ...
 // Extract the required nodes
 // from Document foDoc
 // to String foResult
  XPathFactory xPathfactory = XPathFactory.newInstance();
 XPath xpath = xPathfactory.newXPath();
 XPathExpression expr;
 NodeList body = null;
 String foResult;
 String xp = "/*[local-name()='root']//descendant::*";
 
 try {
 expr = xpath.compile(xp);
 body = (NodeList) expr.evaluate(foDoc, XPathConstants.NODESET);
 foResult = nodeListToString(body);
 } catch (XPathExpressionException e) {
 logger.error(e);
 foResult = "";
 } catch (TransformerException e) {
 logger.error(e);
 foResult = "";
 }
 // ...

Please disregard any exception handling inadequacies.

The above functionality is a part of Java 6 application. I use org.w3c.dom and javax.xml.transform. Still in the debugging process.

 // ...
 // Extract all child notes of root from foDoc
 // and convert the result to String
 NodeList body = foDoc.getChildNodes();
 String foResult;
 
 try {
 foResult = nodeListToString(body);
 } catch (TransformerException e) {
 logger.error(e);
 foResult = "";
 }
 // ...

Please disregard any exception handling inadequacies.

Edited on 10/06/2015

Since my originally posted code had a "just plain wrong" part, I've fixed it. There were no answers based on the old code.

Tweeted twitter.com/StackCodeReview/status/650614036472692736
Source Link
PM 77-1
  • 597
  • 2
  • 11
  • 24
Loading
default

AltStyle によって変換されたページ (->オリジナル) /