简体   繁体   中英

Why does Transformer return &lt and &gt instead of < and >?

trnsformer.transform(DomSource, streamResult);

Input in DomSource contains many <br> tags, but instead I get &gt and &lt instead of < and > <br> return as &lt br &gt

I know &lt &gt are equivalent to <> . How can I make transformer class to change the encoding and return <br> instead ?

XML creator

public class CreatXML 
{ 

  public static void main(String[] args){

  try {
    File article = new File("article.txt");
    Scanner scan = new Scanner (article);
    StringBuilder str = new StringBuilder();
    while (scan.hasNext())
    { 
      str.append(scan.nextLine());
      str.append("<br>");
    }
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
    DocumentBuilder builder = factory.newDocumentBuilder();
    Document doc = builder.newDocument();

    Element body  = doc.createElement("div");
    doc.appendChild(body);

    Attr classAttr = doc.createAttribute("class");
    classAttr.setValue("code");
    body.setAttributeNode(classAttr);

    Element p = doc.createElement("p");
    p.appendChild(doc.createTextNode(str.toString()));
    body.appendChild(p);

    TransformerFactory transFatory = TransformerFactory.newInstance();
    Transformer transformer = transFatory.newTransformer();
    DOMSource dom = new DOMSource(doc);

    StringWriter writer = new StringWriter();
    StreamResult result = new StreamResult(writer);
    transformer.transform(dom, result);
    System.out.println(writer.toString());

  }catch (Exception e){e.printStackTrace();}  
  }
}

input sample

<br> this is an input sample <br>

output

<?xml [stuff] ><div><p>&lt;br&gt; this is an input sample &lt;br&gt;&lt;br&gt;</p></div>

The problem lies here:

p.appendChild(doc.createTextNode(str.toString()));

You don't have any <br> elements in your Document. You have a single <p> element whose textual content contains occurrences of the four characters < , b , r , and > . In accordance with well-formed XML, those characters are being encoded in the manner you're seeing.

In other words, createTextNode does not create XML elements.

Instead of a StringBuilder, you'll need to create separate text nodes and element nodes:

while (scan.hasNext()) {
    p.appendChild(doc.createTextNode(scan.nextLine()));
    p.appendChild(doc.createElement("br"));
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM