简体   繁体   中英

read and replace tag in xsl-fo file

I need to read, find tag and replace it in .fo file in java. Please help me to find out how? I have read some topics, but I am new in java, and having some problems... For this file I have to find <fo:external-graphic src="url('Images/box.jpg')"/> tag and replace in it url="(another path)"!

 <fo:block font-weight="bold" space-before.optimum="12pt" space-after.optimum="12pt" padding="0.1in" border="thin solid black">
      The manufacturer declines every liability with regard to any direct or 
      consequential damage caused by the equipment to you, your body parts, 
      your personal belongings, your domestic animals e/o beloved relatives. 
      Use this equipment at your own risk, and let God protect your fingers! 
    </fo:block>

    <fo:block space-before.optimum="6pt">Have fun with our stuff!</fo:block>

  </fo:block><fo:block id="d0e81" text-align="justify" font="11pt Times" line-height="1.3" space-before.minimum="18pt" space-before.conditionality="retain">
    <fo:block font="bold 14pt Helvetica" keep-together.within-column="always" keep-with-next.within-column="always" space-before.minimum="6pt" space-before.optimum="12pt" space-before.conditionality="retain" space-after.optimum="3pt" background-color="silver" padding="3pt" border-top="thin solid black" border-bottom="thin solid black">B.  Unpacking &amp; Installing </fo:block>
    <fo:block space-before.optimum="6pt"> 
      The universal hammer comes shipped in a <fo:wrapper font-style="italic" color="blue" rx:key="carton box">carton box</fo:wrapper> 

  (see Fig. 1)
.
    </fo:block><fo:block margin="3pt" border="thin ridge silver" padding="3pt" space-before.optimum="6pt" space-after.optimum="6pt" text-align="center" font-style="italic" font-family="Helvetica" keep-together.within-column="always"><fo:block text-align="center">
    <fo:external-graphic src="url('Images/box.jpg')"/>
    </fo:block>
      Fig. 1.
      Shipping Box.</fo:block> 

Create a DOM and then get all elements called fo:external-graphic . You can then change the src attribute to whatever you like. Here is some sample code to get you started:

//parse the xml file
Document doc = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new File("file.xml"));

//get all elements called fo:external-graphic
NodeList list = doc.getElementsByTagName("fo:external-graphic");
for(int i = 0 ; i < list.getLength() ; i++){
    Element element = (Element)list.item(i);
    NamedNodeMap attributes = element.getAttributes();

    //change the src attribute
    Attr src = (Attr)attributes.getNamedItem("src");
    System.out.println(src.getValue());
    src.setValue("url('another/path/box.jpg')");
}

//let's print out the resulting doc for debugging purposes
OutputFormat format = new OutputFormat(doc);
format.setLineWidth(65);
format.setIndenting(true);
format.setIndent(2);
Writer out = new StringWriter();
XMLSerializer serializer = new XMLSerializer(out, format);
serializer.serialize(doc);
System.out.println(out.toString());

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